# undefined method \`posts\_path'

**URL:** <https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068>\
**Category:** rubyonrails-talk\
**Created:** [May 20, 2011, 2:22pm UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068 "2011-05-20T14:22:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 20, 2011, 2:22pm UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/1 "2011-05-20T14:22:08Z")

</div>

I tried to generate a form it says "undefined method `posts\_path' for #\<#\<Class:0xb6784798\>:0xb67817f0\>" The following code at line number 2 gives the error.

\<%= link\_to 'New Post'%\> \<%= form\_for(@post) do |f| %\> \<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :name %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :name %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :title %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :title %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :content %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_area :content %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="actions"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.submit %\> &nbsp;&nbsp;\</div\> \<% end %\>

John

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [May 20, 2011, 2:34pm UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/2 "2011-05-20T14:34:35Z")

</div>

Please run rake routes and post that here, or just read through it and see whether posts\_path is listed. What does your routes.rb look like?

Walter

---

<div class="post-metadata">

**Author:** ![Jatin1](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Jatin1](https://discuss.rubyonrails.org/u/Jatin1)\
**Post date:** [May 20, 2011, 2:35pm UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/3 "2011-05-20T14:35:14Z")

</div>

Check your routes file for the routes for **post**. If you scaffolded the model, then there should be resources :post in your routes.rb file.

If the above is not there, just add the line and you will get the routes for CRUD automatically.

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 21, 2011, 4:18am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/4 "2011-05-21T04:18:08Z")

</div>

> Please run rake routes and post that here

&nbsp;&nbsp;rake routes shows &nbsp;&nbsp;posts\_index GET /posts/index(.:format) {:action=\>"index", :controller=\>"posts"}

What does your routes.rb look like? router.rb file looks like:

Check::Application.routes.draw do &nbsp;&nbsp;get "posts/index"

&nbsp;&nbsp;# The priority is based upon order of creation: &nbsp;&nbsp;# first created -\> highest priority.

&nbsp;&nbsp;# Sample of regular route: &nbsp;&nbsp;# match 'products/:id' =\> 'catalog#view' &nbsp;&nbsp;# Keep in mind you can assign values other than :controller and :action

&nbsp;&nbsp;# Sample of named route: &nbsp;&nbsp;# match 'products/:id/purchase' =\> 'catalog#purchase', :as =\> :purchase &nbsp;&nbsp;# This route can be invoked with purchase\_url(:id =\> product.id)

&nbsp;&nbsp;# Sample resource route (maps HTTP verbs to controller actions automatically): &nbsp;&nbsp;# resources :products

&nbsp;&nbsp;# Sample resource route with options: &nbsp;&nbsp;# resources :products do &nbsp;&nbsp;# member do &nbsp;&nbsp;# get 'short' &nbsp;&nbsp;# post 'toggle' &nbsp;&nbsp;# end

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [May 21, 2011, 8:05am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/5 "2011-05-21T08:05:13Z")

</div>

Have a look at the Rails Guide on Routing to see how routing works. Read it through carefully and make sure you understand it. Then you will easily answer your question yourself.

Colin

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 21, 2011, 9:00am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/6 "2011-05-21T09:00:11Z")

</div>

Ok Colin.I will.But still can you help now to resolve it?

John

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [May 21, 2011, 9:13am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/7 "2011-05-21T09:13:06Z")

</div>

Did you try Jatin Kumar's suggestion?

Colin

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 21, 2011, 3:22pm UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/8 "2011-05-21T15:22:21Z")

</div>

yes i added a resource and now router.rb look likes but same issue is still there

Check::Application.routes.draw do &nbsp;&nbsp;get "posts/index" resources :post &nbsp;&nbsp;# The priority is based upon order of creation: &nbsp;&nbsp;# first created -\> highest priority.

&nbsp;&nbsp;# Sample of regular route: &nbsp;&nbsp;# match 'products/:id' =\> 'catalog#view' &nbsp;&nbsp;# Keep in mind you can assign values other than :controller and :action

&nbsp;&nbsp;# Sample of named route: &nbsp;&nbsp;# match 'products/:id/purchase' =\> 'catalog#purchase', :as =\> :purchase &nbsp;&nbsp;# This route can be invoked with purchase\_url(:id =\> product.id)

&nbsp;&nbsp;# Sample resource route (maps HTTP verbs to controller actions automatically): &nbsp;&nbsp;# resources :products

&nbsp;&nbsp;# Sample resource route with options: &nbsp;&nbsp;# resources :products do &nbsp;&nbsp;# member do &nbsp;&nbsp;# get 'short' &nbsp;&nbsp;# post 'toggle' &nbsp;&nbsp;# end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 21, 2011, 6:45pm UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/9 "2011-05-21T18:45:49Z")

</div>

resources should be plural, ie resources :posts

Fred

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 22, 2011, 6:41am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/10 "2011-05-22T06:41:44Z")

</div>

Thank you very much.It worked.But now i got another error in following file ,it says

"undefined method `name' for #\<Post id: nil, created\_at: nil, updated\_at: nil\>"

John

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [May 22, 2011, 6:55am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/11 "2011-05-22T06:55:03Z")

</div>

Look at the error and try and work out what it means. There should also be an indication of which line generated the error. It says undefined method 'name', so that suggests you are calling the method name for something. It tells you that the object you are calling it on is a Post object. Have a look at your code, can you see where the error is being generated? Assuming that you can see that you are calling name on a Post object, does Post have a method called name? Assuming that Post is an ActiveRecord model then is the posts table supposed to have a name column? If so does it have such a column?

Colin

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 22, 2011, 7:09am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/12 "2011-05-22T07:09:10Z")

</div>

Error is generated at link 8 in following code.

\<%= link\_to 'New Post', new\_post\_path%\> \<%= form\_for(@post) do |f| %\>

&nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :name %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :name %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :title %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :title %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :content %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_area :content %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="actions"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.submit %\> &nbsp;&nbsp;\</div\> \<% end %\>

&nbsp;&nbsp;&nbsp;Posts\_controller.rb file looks like: class PostsController \< ApplicationController &nbsp;&nbsp;def try &nbsp;&nbsp;@post = Post.new &nbsp;&nbsp;end def create @post = Post.new(params[:post]) respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end end

John

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [May 22, 2011, 7:15am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/13 "2011-05-22T07:15:42Z")

</div>

Here you are asking to display (and allow entry) of @post.name, whereas the error suggests that Post does not have a method 'name'.

Colin

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 22, 2011, 4:23pm UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/14 "2011-05-22T16:23:05Z")

</div>

'name' is field that i want to generate in form.Let me tell you i have total 3 files

1)posts\_controller.rb(code is above)

2)try.html.erb(/view/posts)(code is above)

3)post.rb(Model)(Empty)

John

---

<div class="post-metadata">

**Author:** ![Jatin1](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Jatin1](https://discuss.rubyonrails.org/u/Jatin1)\
**Post date:** [May 22, 2011, 4:57pm UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/15 "2011-05-22T16:57:36Z")

</div>

You are confusing things a lot. Answer this question first: Did you scaffold the model or you are adding the files one by one writing the code by hand.

1. If you did scaffold, I don’t see a problem coming anywhere.

2. If you didn’t scaffold, then go for Colin’s suggestion and read the guides, you will understand how Rails works. AND read the guides rightnow, before you get troubled by another problem.

As of the current problem that you are facing, do this:

1. Look up the database table for posts, and see if you have a column named ‘name’ in there. If not, what went wrong.
2. I am quite sure, 1st would fail. Now, look up your post.rb model file and see if there is

attr\_accessible : name in your model file.

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 23, 2011, 1:59am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/16 "2011-05-23T01:59:49Z")

</div>

> You are confusing things a lot. Answer this question first: Did you scaffold the model or you are adding the files one by one writing the code by hand.

I didnt scaffold.I am adding files one by one.

> 1. If you did scaffold, I don't see a problem coming anywhere. 2. If you didn't scaffold, then go for Colin's suggestion and read the guides, you will understand how Rails works. AND read the guides rightnow, before you get troubled by another problem.
> 
> As of the current problem that you are facing, do this: 1. Look up the database table for posts, and see if you have a column named 'name' in there. If not, what went wrong.

In 'posts' table there is no column for name,title and content.

> 2. I am quite sure, 1st would fail. Now, look up your post.rb model file and see if there is attr\_accessible : name in your model file.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;It is not there,but after adding it there ,error didnt resolve.

John

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [May 23, 2011, 7:04am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/17 "2011-05-23T07:04:00Z")

</div>

I suggested before reading the Rails Guide on Routing. I also suggest that you read the Getting Started guide and then the others. Also work through a good tutorial (make sure it is for the version of Rails that you are using). railstutorial.org is a good free to use online tutorial. The reason is that you are missing some fundamental understanding of how Rails works. When you do form\_for(@post) and then f.text\_field :name rails expects @post to have a method 'name' to get the initial value for display. Often this will come automatically from the fact that the posts table will contain a column 'name'. Since you have no such column it complains that Post has no method 'name'.

Colin

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 23, 2011, 10:20am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/18 "2011-05-23T10:20:38Z")

</div>

> > 
> 
> > 
> 
> > > You are confusing things a lot.
> 
> > > Answer this question first:
> 
> > > Did you scaffold the model or you are adding the files one by one writing
> 
> > > the code by hand.
> 
> > I didnt scaffold.I am adding files one by one.
> 
> > 
> 
> > > 1. If you did scaffold, I don’t see a problem coming anywhere.
> 
> > > 1. If you didn’t scaffold, then go for Colin’s suggestion and read the
> 
> > > guides, you will understand how Rails works.
> 
> > > AND read the guides rightnow, before you get troubled by another problem.
> 
> > >
> 
> > > As of the current problem that you are facing, do this:
> 
> > > 1. Look up the database table for posts, and see if you have a column named
> 
> > > ‘name’ in there. If not, what went wrong.
> 
> > In ‘posts’ table there is no column for name,title and content.

> I suggested before reading the Rails Guide on Routing. I also suggest
> 
> that you read the Getting Started guide and then the others. Also
> 
> work through a good tutorial (make sure it is for the version of Rails
> 
> that you are using). [railstutorial.org](http://railstutorial.org) is a good free to use online
> 
> tutorial. Thank you very much.I will read it now.

> The reason is that you are missing some fundamental
> 
> understanding of how Rails works. When you do form\_for(@post) and
> 
> then f.text\_field :name rails expects @post to have a method ‘name’ to
> 
> get the initial value for display. Often this will come automatically
> 
> from the fact that the posts table will contain a column ‘name’.
> 
> Since you have no such column it complains that Post has no method Because i am not doing scaffold,so can i add column values

manually to table ‘posts’ OR changes should be made in

Model.

John

John

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [May 23, 2011, 10:38am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/19 "2011-05-23T10:38:49Z")

</div>

You can add them using migrations. railstutorial.org will show you how to do that.

Colin

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.rubyonrails.org/u/John16)\
**Post date:** [May 23, 2011, 10:50am UTC](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068/20 "2011-05-23T10:50:49Z")

</div>

class CreatePosts \< ActiveRecord::Migration

def self.up

create\_table :posts do |t|

t.string :name

t.string :title

t.text :content

t.timestamps

end

end

def self.down

drop\_table :posts

end

end

John

[Next page](https://discuss.rubyonrails.org/t/undefined-method-posts-path/59068.md?page=2)
