Reputation: 979
I read a book called 'Rails 3 in Action' and made two pages: 'index' and 'new', set routes.rb:
root :to => 'projects#index'
match '/new', to:'projects#new'
and projects_controller:
def new
@project = Project.new
end
def create
@project = Project.new(parmas[:project])
@project.save
flash[:notice] = "Project has been created"
redirect_to @project
end
and view files:
index.html.erb
<%= link_to "new", new_path %>
This works correctly, because I end up at localhost:3000/new
, but the problem is:
<%= form_for(@project) do |f| %>
This results in:
undefined method `projects_path' for #<#:0x416b830>
Where is projects_path? When I print <%= root_path %>
, I get /
, but <%= projects_path %>
gives error undefined method.
How do I define a method projects_path
? Root is not projects_path
?
Upvotes: 0
Views: 1362
Reputation: 239220
The path used to create new projects is a HTTP post to the index URL of "/projects". You need to specify this route:
post "/projects", :controller => "projects", :action => "create", :as => "projects"
This will generate a projects_path
, which is the helper method your form_for
is looking for.
As others have said, you should probably use resources :projects
instead. If you're interested in only creating a subset of the seven RESTful routes created by default, you can use :only
:
resources :projects, :only => %w(index new create)
Upvotes: 0
Reputation: 21791
You should define resource for project in routes.rb
resources :projects
This will generate helper projects_path
and a banch of others
Upvotes: 1
Reputation: 27374
Remove the line match '/new', to:'projects#new'
from routes.rb
and add this:
resources :projects
Upvotes: 0