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

<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit %>
<% end %>

John

On May 20, 2011, at 10:22 AM, John shelfer wrote:

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

On 21 May 2011 05:18, John shelfer [email protected] wrote:

get “posts/index”

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

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

collection do

get ‘sold’

end

end

Sample resource route with sub-resources:

resources :products do

resources :comments, :sales

resource :seller

end

Sample resource route with more complex sub-resources

resources :products do

resources :comments

resources :sales do

get ‘recent’, :on => :collection

end

end

Sample resource route within a namespace:

namespace :admin do

# Directs /admin/products/* to Admin::ProductsController

# (app/controllers/admin/products_controller.rb)

resources :products

end

You can have the root of your site routed with “root”

just remember to delete public/index.html.

root :to => “welcome#index”

See how all your routes lay out with “rake routes”

This is a legacy wild controller route that’s not recommended for

RESTful applications.

Note: This route will make all actions in every controller

accessible via GET requests.

match ‘:controller(/:action(/:id(.:format)))’

end

John

On 21 May 2011 10:00, john shelfer [email protected] wrote:

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

Did you try Jatin K.'s suggestion?

Colin

On Sat, May 21, 2011 at 4:05 AM, Colin L. [email protected]
wrote:

Check::Application.routes.draw do
get “posts/index”

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.

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

 John

On May 21, 5:13am, Colin L. [email protected] wrote:

What does your routes.rb look like?
Ok Colin.I will.But still can you help now to resolve it?
John

Did you try Jatin K.'s suggestion?
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

collection do

get ‘sold’

end

end

Sample resource route with sub-resources:

resources :products do

resources :comments, :sales

resource :seller

end

Sample resource route with more complex sub-resources

resources :products do

resources :comments

resources :sales do

get ‘recent’, :on => :collection

end

end

Sample resource route within a namespace:

namespace :admin do

# Directs /admin/products/* to Admin::ProductsController

# (app/controllers/admin/products_controller.rb)

resources :products

end

You can have the root of your site routed with “root”

just remember to delete public/index.html.

root :to => “welcome#index”

See how all your routes lay out with “rake routes”

This is a legacy wild controller route that’s not recommended for

RESTful applications.

Note: This route will make all actions in every controller

accessible via GET requests.

match ‘:controller(/:action(/:id(.:format)))’

end

John

On May 21, 4:22pm, John shelfer [email protected] wrote:

On May 21, 5:13am, Colin L. [email protected] wrote:

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

resources should be plural, ie resources :posts

Fred

On May 21, 2:45pm, Frederick C. [email protected]
wrote:

resources should be plural, ie resources :posts
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

On May 22, 2:55am, Colin L. [email protected] wrote:

Check::Application.routes.draw do
Look at the error and try and work out what it means. There should
also be an indication of which line generated the error.
Error is generated at link 8 in following code.

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

<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_area :content %>
<%= f.submit %>
<% 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

On 22 May 2011 07:41, John shelfer [email protected] wrote:

Check::Application.routes.draw do
get “posts/index”
resources :post

resources should be plural, ie resources :posts
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>”

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

On 22 May 2011 08:09, John shelfer [email protected] wrote:

Error is generated at link 8 in following code.

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

<%= f.label :name %>
<%= f.text_field :name %>

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

On May 22, 3:15am, Colin L. [email protected] wrote:

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

<%= f.label :name %>
<%= f.text_field :name %>

Here you are asking to display (and allow entry) of @post.name,
whereas the error suggests that Post does not have a method ‘name’.
‘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.

On May 22, 12:57pm, Jatin kumar [email protected] wrote:

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

On 23 May 2011 02:59, John shelfer [email protected] wrote:

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

On 23 May 2011 11:20, john shelfer [email protected] wrote:

Did you scaffold the model or you are adding the files one by one
As of the current problem that you are facing, do this:

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

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

Colin

On Mon, May 23, 2011 at 6:38 AM, Colin L. [email protected]
wrote:

You are confusing things a lot.
AND read the guides rightnow, before you get troubled by another
work through a good tutorial (make sure it is for the version of Rails
Since you have no such column it complains that Post has no method

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

On 23 May 2011 11:50, john shelfer [email protected] wrote:

On 23 May 2011 02:59, John shelfer [email protected] wrote:

  1. If you did scaffold, I don’t see a problem coming anywhere.
    ‘name’ in there. If not, what went wrong.
    The reason is that you are missing some fundamental
    You can add them using migrations. railstutorial.org will show you
    how to do that.
    I created migration and added following code inside and then did
    “rake db:migrate” but still the same error persists.I think now it might
    work,but it did not.

Have you worked right through railstutorial.org already?

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs