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 trialDanish Saleem
7,965 Pointsundefined 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
Pragadesh Krishnan
3,020 PointsI 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
Treehouse TeacherPragadesh 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
Treehouse TeacherPragadesh 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
3,020 PointsThank 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
Treehouse TeacherPragadesh Krishnan Sorry, but I won't know how to help you until I see the exact error you got!
2 Answers
Ethan Rivas
9,979 PointsSorry I didn't get it, when you get that error and when it does work?
chase dougherty
11,536 PointsI had to use <%= form_for :post do |f| %>
Jay McGavren
Treehouse TeacherJay McGavren
Treehouse TeacherDanish Saleem I think you're missing a route that you need. Can you post your entire
routes.rb
file? Can you runbin/rails routes
at the command line and post the result of that?