rails:
我有使用相同部分'_form‘的'create’和'update‘
但是表单应该发送到的url不同。
rails生成的代码将@xxx作为参数提供给form_for
我猜rails会根据内容是否为空来判断发送到'create‘还是'update’?
但我使用的是嵌套资源。rails似乎不能很好地处理这个问题。
那么,我的最佳实践是什么?
把'if‘放在_form里?
create和update同时使用一个局部模板_form
但是表单提交的url不一样
rails自动生成的直接给form_for给了个@xxx做参数我猜是按内容是否为空判断是是否创建还是更新的?
但是我的情况是个嵌套的资源
rails似乎没有聪明到这种程度?
那么我的最佳实践是什么?
在_form中写逻辑判断么?
路由:
book_structures GET /books/:book_id/structures(.:format) {:controller=>"structures", :action=>"index"}
POST /books/:book_id/structures(.:format) {:controller=>"structures", :action=>"create"}
new_book_structure GET /books/:book_id/structures/new(.:format) {:controller=>"structures", :action=>"new"}
edit_book_structure GET /books/:book_id/structures/:id/edit(.:format) {:controller=>"structures", :action=>"edit"}
book_structure GET /books/:book_id/structures/:id(.:format) {:controller=>"structures", :action=>"show"}
PUT /books/:book_id/structures/:id(.:format) {:controller=>"structures", :action=>"update"}_form:
<%if (form_method == :post) %>
<%using_url = book_structures_path%>
<%elsif (form_method == :put) %>
<%using_url = book_structure_path%>
<%end%>
<%= form_for(@structure,:url => using_url,:method => form_method) do |f| %>发布于 2012-02-07 20:35:37
你可以使用
<%= form_for([@book, @structure] ,:method => form_method) do |f| %>若要构建嵌套资源的表单,请执行以下操作。
https://stackoverflow.com/questions/9172979
复制相似问题