开发者

Rails 3 rendering form partial in another controller

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 "开发者_C百科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!


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜