herited_resources,渲染验证错误的正确方法

发布于 2024-12-09 15:48:02 字数 1026 浏览 0 评论 0原文

我用谷歌搜索了很长时间,但没有找到我的问题的完整答案。

在我的应用程序中,我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃安静 2024-12-16 15:48:02

如果没有答案,我认为这没关系,也许渲染是渲染,但操作已经完成并且 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文