Rails—用户在一个表单上为多个模型输入—如何

时间:2022-12-01 10:37:06

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.

这基本上是一个嵌套的表单问题,尽管只有一个字段属于父模型。我的数据输入表单为一个模型收集数据——但是我还需要相互收集一个数据元素/值(UserID),它实际上进入一个父记录,该记录将与细节记录一起创建。

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.

AFAIK Rails期望每个表单字段映射到一个模型,我需要创建一个我将单独使用的未绑定数据输入字段。

How can I override this default behaviour and create a'free form/unbound field'?

如何覆盖此默认行为并创建“*窗体/未绑定字段”?

TIA, BC

TIA,公元前

2 个解决方案

#1


43  

Heres something from my own app:

以下是我自己的应用:

Access it by:

访问它:

params[:company] and params[:user]

Controller:

控制器:

@company = Company.new
@user = User.new

View:

观点:

<% 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 %>

#2


2  

For the "magic" form <=> model mapping form_for is used. (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)

对于“魔术”表单<=>模型映射form_for使用。(http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)

If you need something out of the current model try using http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

如果需要当前模型之外的内容,可以使用http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

With that you can add tags separate from the model, eg

这样,您就可以添加与模型分离的标记

radio_button_tag

inside the form_for block

在form_for块

#1


43  

Heres something from my own app:

以下是我自己的应用:

Access it by:

访问它:

params[:company] and params[:user]

Controller:

控制器:

@company = Company.new
@user = User.new

View:

观点:

<% 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 %>

#2


2  

For the "magic" form <=> model mapping form_for is used. (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)

对于“魔术”表单<=>模型映射form_for使用。(http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)

If you need something out of the current model try using http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

如果需要当前模型之外的内容,可以使用http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

With that you can add tags separate from the model, eg

这样,您就可以添加与模型分离的标记

radio_button_tag

inside the form_for block

在form_for块