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.
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)
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.
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)
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?
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
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.
If you did scaffold, I don’t see a problem coming anywhere.
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:
Look up the database table for posts, and see if you have a column named ‘name’ in there. If not, what went wrong.
I am quite sure, 1st would fail. Now, look up your post.rb model file and see if there is
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.
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'.