43

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
maxday
  • 1,322
  • 1
  • 16
  • 32

3 Answers3

99

you have a students_controller which corresponds to the resources :students line in your routes.rb. This creates routes that uses the word students like students_path and new_student_path. When using form_for(@record), the url is determined from the objects class. In this case, @record is a User so the path is users_path when the object is a new record and user_path(@record) when the object is persisted. since you don't have a users_controller defined, you need to manually set the url of the form_for to fix this error

form_for @user, url: student_path(@user), html: { method: :put } do |f|

now, 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

# new.html.erb
form_for @user, url: students_path, html: { method: :post } do |f|
  render 'form', f: f

# edit.html.erb
form_for @user, url: student_path(@user), html: { method: :put } do |f|
  render 'form', f: f

# _form.html.erb
f.text_field :name
f.text_field :title
cweston
  • 11,297
  • 19
  • 82
  • 107
jvnill
  • 29,479
  • 4
  • 83
  • 86
  • 1
    having the `form_for` outside the partial is a little icky. Would pass the method and path in as local variables – Dex Mar 15 '14 at 08:50
  • if you call `form_for` outside the partial, you only have to pass the form renderer object. the method and path should be passed to `form_for`. i guess the best example on when this is best used is if you have an admin side and you use the same form for the public side of your app. – jvnill Mar 15 '14 at 14:21
  • what if i am not using the partial or new/edit forms, but still getting the same error – Vipin Verma Feb 13 '16 at 08:41
  • 1
    the stacktrace should point out where `user_path` is being called. start debugging there. – jvnill Feb 14 '16 at 15:38
5

If you are here because of the namespace (like me 10 minutes ago) you'll need this:

For namespaced routes, like admin_post_url:

<%= form_for([:admin, @post]) do |f| %>
 ...
<% end %>

from the doc

adominey
  • 102
  • 1
  • 7
1

i fix it with local variables in render . In rails 5 - form_with tag:

# new.html.erb
  render partial: 'form', locals: {user: @user, url: students_path}

# edit.html.erb
  render partial: 'form', locals: {user: @user, url: student_path(@user)}

# _form.html.erb
form_with  model: user, url: url, local: true do |f|
  f.text_field :name
  f.text_field :title
end
E. Musinov
  • 11
  • 1