Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Rails Routes and Resources Routes to Create Actions View for New Pages

Danish Saleem
Danish Saleem
7,965 Points

undefined method error when using form_for

Hi , i understand that the form_for takes an Model Object , i defined it as below in controller class

def new
    @coffenew = CoffeType.new
end

however when i use <%= form_for (@coffenew) do |c| ......

i got the following error:

undefined method `coffe_types_path' for #<#<Class:0x007fe452f23a80>:0x007fe453e13f18>
Did you mean?  coffe_new_path

it works for me if i use the url argument with form_for as <%= form_for(@coffenew , url: 'new') do |c| %> since my route.rb has

get '/coffe/new' , to: 'coffe#new'

but i do not know why i am getting this error and why it is not working the same as in video, need help plz

Jay McGavren
Jay McGavren
Treehouse Teacher

Danish Saleem I think you're missing a route that you need. Can you post your entire routes.rb file? Can you run bin/rails routes at the command line and post the result of that?

Pragadesh Krishnan
Pragadesh Krishnan
3,020 Points

I too get the same error

CONTROLLER FILE

def edit
        @cric_prof = CricProfile.find(params[:id])

edit.html.erb
%= form_for(@cric_prof) do |f| %>

router.rb
Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  get '/cric_profiles', to: 'cric_profiles#index', as: 'profiles'
 # post '/cric_profiles', to: 'cric_profiles#add', as: add_new_player
  get '/cric_profiles/new', to: 'cric_profiles#new'
  get '/cric_profiles/:id', to: 'cric_profiles#show', as: 'cric_prof_show'
  get '/cric_profiles/:id/edit', to: 'cric_profiles#edit'
  post '/cric_profiles', to: 'cric_profiles#add' 
  delete '/cric_profiles/:id', to: 'cric_profiles#delete', as: 'cricDel'

end
Jay McGavren
Jay McGavren
Treehouse Teacher

Pragadesh Krishnan Can you copy the exact error you're getting and paste it here? For example, Danish got the error

undefined method `coffe_types_path' for #<#<Class:0x007fe452f23a80>:0x007fe453e13f18>
Did you mean?  coffe_new_path

above. It looks like he was missing a coffe_types_path method. What method does it report as missing for you?

Jay McGavren
Jay McGavren
Treehouse Teacher

Pragadesh Krishnan I notice you're using some non-standard route names. form_for expects particular path helper methods to exist, and using non-standard route names could lead to conflicts. Your routes should look roughly like this:

Rails.application.routes.draw do
  get    '/cric_profs',          to: 'cric_profs#index'
  post   '/cric_profs',          to: 'cric_profs#create'
  get    '/cric_profs/new',      to: 'cric_profs#new',  as: 'new_cric_prof'
  get    '/cric_profs/:id',      to: 'cric_profs#show', as: 'cric_prof'
  get    '/cric_profs/:id/edit', to: 'cric_profs#edit', as: 'edit_cric_prof'
  patch  '/cric_profs/:id',      to: 'cric_profs#update'
  delete '/cric_profs/:id',      to: 'cric_profs#destroy'
end
Pragadesh Krishnan
Pragadesh Krishnan
3,020 Points

Thank you Jay for your clarification. So Would i have avoided the error if i used the instance variable in the controller file something like this def edit @cric_profile = CricProfile.find(params[:id])

Jay McGavren
Jay McGavren
Treehouse Teacher

Pragadesh Krishnan Sorry, but I won't know how to help you until I see the exact error you got!

2 Answers

Ethan Rivas
Ethan Rivas
9,979 Points

Sorry I didn't get it, when you get that error and when it does work?

I had to use <%= form_for :post do |f| %>