0

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?

3 Answers 3

1

You should define resource for project in routes.rb

resources :projects

This will generate helper projects_path and a banch of others

2
  • solved problem but undefined method `label=' for #<ActionView::Helpers::FormBuilder:0x4171608> where i can define Formbuilder ? Commented Aug 16, 2012 at 4:17
  • @ChangJuPark You don't have to "define FormBuilder". You need to use <%= f.label :my_field %>. It sounds like you're using <% label= ... %> instead, which is wrong.
    – user229044
    Commented Aug 16, 2012 at 4:24
0

Remove the line match '/new', to:'projects#new' from routes.rb and add this:

resources :projects
0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.