rails:
I have ‘create’ and ‘update’ using the same partial ‘_form’
but the url that the form should send to is different
I code generated by rails gives an @xxx as parameter to form_for
I guess rails will judge to send to ‘create’ or ‘update’ by whether the content is empty?
But I`m using a nesting resource.
rails seems not so smart to handle this.
So,what is my best practice?
put ‘if’ in _form?
create 和 update 同时使用一个局部模板 _form
但是表单提交的url不一样
rails自动生成的直接给form_for给了个@xxx做参数 我猜是按内容是否为空判断是create还是update的?
但是我的情况是个嵌套的资源
rails似乎没有聪明到这种程度?
那么我的最佳实践是什么?
在_form中写逻辑判断么?
routes:
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| %>
You could use
to build a form for nested resource.