herited_resources,渲染验证错误的正确方法
我用谷歌搜索了很长时间,但没有找到我的问题的完整答案。
在我的应用程序中,我使用inherited_resources。因此,我想在成功创建 Comment 对象后覆盖重定向,所以我写道:
update! do |success, failure|
success.html { redirect_to parent_url }
end
我知道我可以在一个字符串中完成它,但我需要块,因为我也需要在失败发生时添加操作。所以我写道:
update! do |success, failure|
success.html { redirect_to parent_url }
failure.html {
add_breadcrumb t("share.Home"), root_path
add_breadcrumb t("project.Projects"), projects_path
...
add_breadcrumb t("comment.edit_comment"), edit_resource_url
render :action => :edit and return }
end
当一切正常时,它会正确重定向,但有任何错误我没有得到编辑操作的 URL,我有显示操作 URL,但会显示编辑表单和错误。这没关系还是我做错了什么? 这是我提供的附加信息表格:
<% link = "project_#{@comment.commentable_type.downcase}_comment_path"%>
<%= semantic_form_for [@project,@comment.commentable,@comment], :validate => true, :url => send(link,@project,@comment.commentable,@comment), do |f| %>
<%= render 'form', :f => f %>
<% end %>
I googled for a long time, but didn't find full answer for my problem.
In my app I use inherited_resources. So, I want to override redirect after succefull creation of Comment object, so I write:
update! do |success, failure|
success.html { redirect_to parent_url }
end
I know that I could do it in one string, but I need block, because I need to add actions when failure happens too. So I write:
update! do |success, failure|
success.html { redirect_to parent_url }
failure.html {
add_breadcrumb t("share.Home"), root_path
add_breadcrumb t("project.Projects"), projects_path
...
add_breadcrumb t("comment.edit_comment"), edit_resource_url
render :action => :edit and return }
end
And when everything is OK it redirects right, but there are any errors I don't get URL of edit action, I have show action URL, but edit form and errors are displayed. Is this all right or am I doing anything wrong?
Here is my rendering of form for additional information:
<% link = "project_#{@comment.commentable_type.downcase}_comment_path"%>
<%= semantic_form_for [@project,@comment.commentable,@comment], :validate => true, :url => send(link,@project,@comment.commentable,@comment), do |f| %>
<%= render 'form', :f => f %>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有答案,我认为这没关系,也许渲染是渲染,但操作已经完成并且 URL 已更改。不管怎样,谢谢。
If there is no answers I think that this is all right, maybe render is render, but action is already done and URL changed. Anyway, thanks.