I am trying to allow comments on a nested resource and I am getting an undefined method ‘comment’ for # in app/controllers/comments_controller.rb:7:in `create’ when i select the create comment button. As I’m still new to ruby and rails, I followed the code in the getting started guide and I can’t seem to figure out why the error.
Associations are:
has_many :comments
belongs_to :restaurant
In my routes
resources :restaurants do
resources :comments
end
In the comments controller
def create
@restaurant = Restaurant.find(params[:restaurant_id])
@comment = @restaurant.comments.create(params[:comment])
redirect_to restaurant_path
end
In my restaurant show template
<%= form_for([@restaurant, @restaurant.comments.build]) do |f| %>
<h2>Add a comment</h2>
<div>
<%= f.text_area :body %>
</div>
<div>
<%= f.submit %>
<% end %>
You should do the following on the first line in your create statement
You should see something like the following
The rest of your site should look something like the following
comments_controller.rb
comments.rb
restaurants.rb
You probably missed a step in guide. Everything else looks to be alright.