I'm getting an error :
undefined method `user_path' for #<#<Class:0x007fd1f223ead0>:0x007fd1f2241af0>
when I'm trying to edit a student. I don't really understand the "user_path" method alert since I never write this in the view. (Student is not model) and I didn't use rails g scaffold to generate it.
Thanks
In my StudentsController :
def edit
@student = User.find(params[:id])
end
In the view (edit.html.erb) :
<%= form_for(@student) do |f| %> ...
In routes.rb :
resources :students
Best Answer
you have a
students_controller
which corresponds to theresources :students
line in yourroutes.rb
. This creates routes that uses the wordstudents
likestudents_path
andnew_student_path
. When usingform_for(@record)
, the url is determined from the objects class. In this case,@record
is aUser
so the path isusers_path
when the object is a new record anduser_path(@record)
when the object is persisted. since you don't have ausers_controller
defined, you need to manually set the url of theform_for
to fix this errornow, if you're using a partial called
_form.html.erb
and uses this on both the new and edit actions, you're going to have a problem since the urls for both new and edit actions are different. you have to change your views to something like this