Rails 3在另一个控制器中呈现部分形式
我有一个命名空间和嵌套资源(“博客/帖子/评论”)的表单部分,到目前为止,正常工作一直很痛苦。我设法让它适用于“新”和“编辑”操作(在评论控制器中),但现在我想将其加载到父控制器(帖子)的“显示”视图中。不过,我尝试过的任何方法都行不通。
以下是在“/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"}
我已经用这个方法折腾了一个星期了,所以非常感谢任何帮助。谢谢!
I have a form partial for a namespaced and nested resource ("blog/posts/comments") which so far has been a pain to get working properly. I managed to get it working for the "new" and "edit" actions (in the Comments controller), but now I want to load it into the "show" view of the parent controller (Posts). Nothing I have tried will work, though.
Here is how the form is generated in "/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| %>
I tried call this partial using a standard "render" command but I get the following error which most likely has to do with the way I generate the URL for the form in the code above:
No route matches {:action=>"create", :controller=>"blog/comments"}
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:425:in `raise_routing_error'
Here are my routes:
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"}
I have been bashing my head off the wall with this for a week now so any help is greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery 来救援!
如果有人知道更好的方法来做到这一点,我洗耳恭听!
jQuery to the rescue!
If anyone knows a better way to do this, I'm all ears!