以form_for为例
<% form_for :product, :url=>{:action=>’create’}, :html=>{:multipart=>true} do |form| %>
<%end%>
?
1 表格为products,假设,字段: name,
<%=form.text_field ‘name’,{},:size=>20%>
???? def create
??????????? product=Product.new(params[:name])
???????????? product.save
???????? end
????? end
?
2?多个动态添加字段? name
?<%=form.text_field ‘name[]’,:size=>20%>
?? 保存:
?????? def create
????????? params[name].each do |name|
????????????? product=Product.new(name)
???????????? product.save
???????? end
?????? end
?
?保存提到model
???? def create
????????? product=Product.batch_build(params[:name])
???? end
??
?? model 方法
?? def self.bath_build name
???????? name.each do |n|
???????????? product=Product.new(n)
?????? ?????? product.save
???????? end
????end
?
3? 不属于procducts表,例如users 中的? phone,但必需在表单提交内
?
???<%=text_filed_tag ‘user[phone]’,{},:size=>20%>
??
?? def create
??????? user=User.new(params[:user][phone])
??????? user.save???
??????? 或是? user.phone=params[:user][:phone]???? user.save
?? end
?
4? 不属于Products表,动态添加字段
???? view:???
??????? ?? <td>?????????????? <%=text_field_tag ‘material[][batch_no]’, nil, :size=>3%> ????????? </td>??????????? <td><%=text_field_tag ‘material[][composit]’, nil, :size=>3%></td>??????????? <td><%=text_field_tag ‘material[][tension]’, nil, :size=>3%></td>
???? controller
????? def create
??????? material_state =MaterialState.build_batch(params[:material])
????? end
?? model
?????? def self.build_batch material?????????? ? material.each do |material|??????????? ?material=MaterialState.new(material)???????????? material.save????? end如果有新的,会及进补充。
希望能我一样的初学者带来帮助?????
?
旅行,其实是需要具有一些流浪精神的,