Rails-单一表单上多个模型的用户输入-如何 [英] Rails - User Input for Multiple models on a single form - How
问题描述
这基本上是一个嵌套表单问题,尽管只有一个字段属于父模型.我的数据输入表单收集了模型的数据-但是我还需要互相收集一个数据元素/值(UserID),该数据元素/值实际上会进入将由明细记录创建的父记录中.
This is basically a nested form question, albeit with only one field that belongs to a parent model. My data entry form collects data for a model - however I also need to collect one other a data element/value (UserID) that actually goes into a parent record that will be created with the detail record.
AFAIK Rails希望每个表单字段都映射到一个模型,我需要创建一个未绑定的数据输入字段,我将单独使用它.
AFAIK Rails expects each form field to map to a model and I need to create an unbound data input field that I will use separately.
如何覆盖此默认行为并创建自由格式/未绑定字段"?
How can I override this default behaviour and create a'free form/unbound field'?
TIA, 公元前
推荐答案
此处包含我自己的应用中的内容:
Heres something from my own app:
通过以下方式访问它:
params[:company] and params[:user]
控制器:
@company = Company.new
@user = User.new
查看:
<% form_for @company, :url => companies_path do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :website %><br />
<%= f.text_field :website %>
</p>
<hr />
<% fields_for @user do |u| %>
<p>
<%= u.label :email %><br />
<%= u.text_field :email %>
</p>
<p>
<%= u.label :username %><br />
<%= u.text_field :username %>
</p>
<p>
<%= u.label :password %><br />
<%= u.password_field :password %>
</p>
<p>
<%= u.label :password_confirmation %><br />
<%= u.password_field :password_confirmation %>
</p>
<% end %>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
这篇关于Rails-单一表单上多个模型的用户输入-如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!