我有一个 Rails 4 应用程序,其 Thing 模型具有 3 个属性:attr_1、attr_2、attr_3。
我想要两个编辑表单:
- 第一个会更新 attr_1 和 attr_2
- 第二个只会更新 attr_3
我在我的 things_controller.rb 中写了四个方法:
def edit
#edit form for attr_1 & attr_2
end
def edit_attr_3
#edit form for attr_3 only
end
def update
#update attr_1 & attr_2
end
def update_attr_3
#update attr_3 only
end
这是我的 routes.db
resources :things do
member do
get :edit_attr_3
put :update_attr_3
end
end
现在,GET 到两个编辑 View 都有效(/things/:id/edit 和/things/:id/edit_attr_3)但是在提交时,thing#update 表单被调用两次(thing#update_attr_3 从未被调用)
请问我如何继续将 thing#edit_attr_3 操作链接到 thing#update_attr_3?
编辑:
回答,感谢Ross & Steve Klein :
在我看来,我正在使用 bootstrap_form_for .通常 :url 参数是可选的,因为标准的编辑/更新是按惯例链接的。在自定义编辑或更新操作的情况下,需要指定更新路径。
<%= bootstrap_form_for @thing, url: update_attr_3_thing_path(@thing) do |f| %>
<% end %>
最佳答案
如果没有看到表单的代码就很难说...但是如果您使用 form_for
那么在这两种情况下它将映射到更新操作的路由(对象不是一个新对象)。
要使其与您独特的更新操作一起使用,您必须指定 form_for
应该使用的 url。像这样的东西:
<%= form_for @thing, url: things_update_attr_3_path(@thing) do |f| %>
# other form stuff .......
您可以在命令提示符处使用 bundle exec rake routes
来查看您当前的路由是什么,以便您现在可以将路由助手与 form for 一起使用。
关于ruby - Rails 4 : Custom routes, 单个模型的多个编辑表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31026186/