sethi
sethi

Reputation: 1889

Unable to create form for reference model

I am following the rails guide

http://guides.rubyonrails.org/getting_started.html

In the example there is controller article and in its show.erb.html I am trying to add a form for comments

    <h2>Add a comment:</h2>
    <%= form_for([@article, @article.comments.build]) do |f| %>
    <p>
      <%= f.label :commenter %><br>
      <%= f.text_field :commenter %>
    </p>
    <p>
      <%= f.label :body %><br>
      <%= f.text_area :body %>
    </p>
  <p>
    <%= f.submit %>
  </p>
<% end %> 

But i get an error

undefined method `article_comments_path' for #<#:0x3a74498>

I am trying to learn rails , think its a small issue but cant figure it out

Upvotes: 0

Views: 45

Answers (2)

Aaditi Jain
Aaditi Jain

Reputation: 7027

It looks like something is missing in your routes.rb file:

Your routes file should have this:

 resources :articles do
   resources :comments 
 end

Also, the relationship in your models should be:

class Article < ActiveRecord::Base
 has_many :comments
end

class Comment< ActiveRecord::Base
 belongs_to :article
end

Upvotes: 1

Rubyrider
Rubyrider

Reputation: 3587

in your route file make sure you have the following:

resources :articles do
  resources :comments
end

I think, this is the only thing to help you with this problem.

Upvotes: 2

Related Questions