开发者

Can't access nested resource 'comments' in rails 3.0.1

I hope you can help me.

/config/routes.rb:

resources :deadlines do  
  resources :comments  
end

/model/comment.rb:

class Comment < ActiveRecord::Base  

  belongs_to :post, :class_name => "Post", :foreign_key => "post_id" 开发者_开发问答 

end  

/model/post.rb:

class Post < ActiveRecord::Base  

  has_many :comments  

end

When I want to visit http://localhost:3000/posts/1/comments/new I get:

undefined method `comments_path' for #<#<Class:0x4889d18>:0x4887138> in _form.html  

I use 'formtastic' and the _form.html.erb looks like this:

<% semantic_form_for [@comment] do |form| %>  
  <% form.inputs do %>  
    <%= form.input :content %>  
  <% end %>  

  <% form.buttons do %>  
    <%= form.commit_button %>  
  <% end %>  
<% end %>


Is your other model Post or Deadline? Assuming it is Post:

resources :posts do
  resources :comments
end

Run rake routes in the terminal to see all your routes. Further info:

  • http://guides.rubyonrails.org/routing.html#seeing-existing-routes-with-rake
  • http://guides.rubyonrails.org/routing.html#paths-and-urls
  • http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects

Syntax for nested resources is:

<% semantic_form_for [@post, @comment] do |form| %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜