undefined method `posts_path'

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">     <%= f.label :name %><br />     <%= f.text_field :name %>   </div>   <div class="field">     <%= f.label :title %><br />     <%= f.text_field :title %>   </div>   <div class="field">     <%= f.label :content %><br />     <%= f.text_area :content %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %>

John

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

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.

Please run rake routes and post that here

  rake routes shows   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   get "posts/index"

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

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

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

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

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

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

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

John

Did you try Jatin Kumar's suggestion?

Colin

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

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

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

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

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

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

resources should be plural, ie resources :posts

Fred

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

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

Error is generated at link 8 in following code.

<%= link_to 'New Post', new_post_path%> <%= form_for(@post) do |f| %>

  <div class="field">     <%= f.label :name %><br />     <%= f.text_field :name %>   </div>   <div class="field">     <%= f.label :title %><br />     <%= f.text_field :title %>   </div>   <div class="field">     <%= f.label :content %><br />     <%= f.text_area :content %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %>

   Posts_controller.rb file looks like: class PostsController < ApplicationController   def try   @post = Post.new   end def create @post = Post.new(params[:post]) respond_to do |format|     end   end end

John

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

'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

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.

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.

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

John

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

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 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

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

Colin

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