Ruby on rails Rails在提交时设计简单的嵌套2个模型
我有一个表单,你可以作为一个普通用户注册,还有一个表单可以在注册后创建一个“公司”。但是我想创建一个“第三个”表单,您可以在其中创建一个公司和一个新用户,所以我需要在两个模型上创建 _form.html.erbRuby on rails Rails在提交时设计简单的嵌套2个模型,ruby-on-rails,ruby,forms,devise,Ruby On Rails,Ruby,Forms,Devise,我有一个表单,你可以作为一个普通用户注册,还有一个表单可以在注册后创建一个“公司”。但是我想创建一个“第三个”表单,您可以在其中创建一个公司和一个新用户,所以我需要在两个模型上创建 _form.html.erb <%= simple_form_for(@company) do |f| %> <% if @company.errors.any? %> <div id="error_explanation"> <h2><%= plurali
<%= simple_form_for(@company) do |f| %>
<% if @company.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@company.errors.count, "error") %> prohibited this company from being saved:</h2>
<ul>
<% @company.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name, :class => "input-txt form-control" %>
</div>
<div class="field">
<%= f.label :address %><br>
<%= f.text_area :address, :class => "input-txt form-control" %>
</div>
<div class="field">
<%= f.label :business_phone %><br>
<%= f.text_field :business_phone, :class => "input-txt form-control" %>
</div>
<div class="field">
<%= f.label :cell_phone %><br>
<%= f.text_field :cell_phone, :class => "input-txt form-control" %>
</div>
<% if user_signed_in? %>
<div class="actions">
<%= f.submit %>
</div>
<% else %>
<% simple_fields_for(@users) do |f| %>
<p><%= f.label :email%><br/>
<%= f.email_field :email, :class => "input-txt form-control" %>
</p>
<p><%= f.label :password %><br />
<%= f.password_field :password, :class => "input-txt form-control" %>
</p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, :class => "input-txt form-control" %>
</p>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% end %>
<% end %>
它应该显示用户输入字段和提交按钮。如果用户尚未登录但未登录,则只显示公司输入字段。如果没有与舾装商相关联的用户记录,我会得到NilClass:Class的“未定义方法`model\u name',
的简单字段将不显示用户。在控制器中,执行以下操作:
def new
@outfitter = Outfitter.new
@outfitter.users.build
end
这将在@outfitter
用户关系中创建一个空白用户记录
然后在您的视图中,将(@user)
的simple\u fields\u更改为simple\u fields\u for:users
同时,将fields\u更改为fields\u for:users do | u
,您可以在:users块内使用u.input
belongs_to :company
def new
@outfitter = Outfitter.new
@outfitter.users.build
end