你有一个students_controller对应resources :students于你的routes.rb. 这将创建使用单词studentslikestudents_path和的路由new_student_path。使用form_for(@record)时,url 由 objects 类确定。在这种情况下,@record是 aUser所以路径是users_path当对象是新记录并且user_path(@record)对象被持久化时。由于您没有users_controller定义,您需要手动设置的 urlform_for来修复此错误
form_for @user, url: student_path(@user), html: { method: :put } do |f|
现在,如果您使用部分调用_form.html.erb并在新操作和编辑操作上都使用它,那么您将遇到问题,因为新操作和编辑操作的 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