Rails 3在另一个控制器中呈现部分形式

发布于 2024-11-14 09:41:47 字数 1726 浏览 2 评论 0原文

我有一个命名空间和嵌套资源(“博客/帖子/评论”)的表单部分,到目前为止,正常工作一直很痛苦。我设法让它适用于“新”和“编辑”操作(在评论控制器中),但现在我想将其加载到父控制器(帖子)的“显示”视图中。不过,我尝试过的任何方法都行不通。

以下是在“/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 技术交流群。

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

发布评论

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

评论(1

蓬勃野心 2024-11-21 09:41:47

jQuery 来救援!

$("div#form").load("<%= new_blog_post_comment_path(@post) -%> #blog_post_comment_form")

如果有人知道更好的方法来做到这一点,我洗耳恭听!

jQuery to the rescue!

$("div#form").load("<%= new_blog_post_comment_path(@post) -%> #blog_post_comment_form")

If anyone knows a better way to do this, I'm all ears!

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