Ruby-on-rails form_for 未定义的方法`user_path'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14740702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:23:24  来源:igfitidea点击:

form_for undefined method `user_path'

ruby-on-railsruby-on-rails-3

提问by Max

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.

当我试图编辑学生时。我不太了解“user_path”方法警报,因为我从未在视图中写过这个。(学生不是模特)我没有使用 rails g scaffold 来生成它。

Thanks

谢谢

In my StudentsController :

在我的 StudentsController 中:

def edit
  @student = User.find(params[:id])
end

In the view (edit.html.erb) :

在视图 (edit.html.erb) 中:

<%= form_for(@student) do |f| %> ...

In routes.rb :

在 routes.rb 中:

resources :students

回答by jvnill

you have a students_controllerwhich corresponds to the resources :studentsline in your routes.rb. This creates routes that uses the word studentslike students_pathand new_student_path. When using form_for(@record), the url is determined from the objects class. In this case, @recordis a Userso the path is users_pathwhen the object is a new record and user_path(@record)when the object is persisted. since you don't have a users_controllerdefined, you need to manually set the url of the form_forto fix this error

你有一个students_controller对应resources :students于你的routes.rb. 这将创建使用单词studentslikestudents_path和 的路由new_student_path。使用时form_for(@record),url 由对象类确定。在这种情况下,@record是一个User所以路径是users_path当对象是新记录user_path(@record)时和对象被持久化时。由于您没有users_controller定义,您需要手动设置 urlform_for以修复此错误

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

now, if you're using a partial called _form.html.erband 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

现在,如果您使用部分调用_form.html.erb并在 new 和 edit 操作上使用它,您将遇到问题,因为 new 和 edit 操作的 url 不同。你必须把你的观点改成这样

# 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

回答by adominey

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

如果你是因为命名空间(就像我 10 分钟前)来到这里,你需要这个:

For namespaced routes, like admin_post_url:

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

对于命名空间路由,如 admin_post_url:

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

from the doc

文档

回答by E. Musinov

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

我在 render 中用局部变量修复它。在 rails 5 - form_with 标签中:

# 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