我有一个命名空间和嵌套资源(“博客/帖子/评论”)的表单部分,到目前为止,正常工作一直很痛苦。我设法让它适用于"new"和“编辑”操作(在评论 Controller 中),但现在我想将其加载到父 Controller (帖子)的“显示” View 中。不过,我尝试过的任何方法都行不通。
以下是在“/blog/comments/shared/_comment_form.html.erb”中生成表单的方式:
<%= form_for([:blog, @post, @comment],
:url => { :controller => :comments, :action => (@comment.persisted? ? :update : :create) },
:html => { :id => "blog_post_comment_form", :class => "standard_form wide" }) do |f| %>
我尝试使用标准“渲染”命令调用此部分,但出现以下错误,这很可能与我在上面的代码中生成表单 URL 的方式有关:
No route matches {:action=>"create", :controller=>"blog/comments"}
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:425:in `raise_routing_error'
这是我的路线:
blog_post_comments GET /blog/posts/:post_id/comments(.:format) {:controller=>"blog/comments", :action=>"index"}
POST /blog/posts/:post_id/comments(.:format) {:controller=>"blog/comments", :action=>"create"}
new_blog_post_comment GET /blog/posts/:post_id/comments/new(.:format) {:controller=>"blog/comments", :action=>"new"}
edit_blog_post_comment GET /blog/posts/:post_id/comments/:id/edit(.:format) {:controller=>"blog/comments", :action=>"edit"}
blog_post_comment GET /blog/posts/:post_id/comments/:id(.:format) {:controller=>"blog/comments", :action=>"show"}
PUT /blog/posts/:post_id/comments/:id(.:format) {:controller=>"blog/comments", :action=>"update"}
DELETE /blog/posts/:post_id/comments/:id(.:format) {:controller=>"blog/comments", :action=>"destroy"}
我已经为此苦苦挣扎了一个星期了,所以非常感谢您的帮助。谢谢!
最佳答案
jQuery 来救援!
$("div#form").load("<%= new_blog_post_comment_path(@post) -%> #blog_post_comment_form")
如果有人知道更好的方法来做到这一点,我洗耳恭听!
关于ruby-on-rails - Rails 3在另一个 Controller 中呈现部分形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274554/