Rails4显示嵌套表单只保存一行

时间:2014-06-04 07:29:21

标签: ruby-on-rails forms ruby-on-rails-4

我需要一个允许用户接受4组输入以邀请四个用户进入实体的表单。以下是代码。

# GET /company_members/new
  def new
    @company_member = CompanyMember.new
    @company_member.company_members.build
  end

  # POST /company_members
  # POST /company_members.json
  def create
    @company_member = CompanyMember.new(company_member_params)
    @company_member.user_id = 1
    @company_member.company_id = 1

    respond_to do |format|
      if @company_member.save
        format.html { redirect_to @company_member, notice: 'Company member was successfully created.' }
        format.json { render action: 'show', status: :created, location: @company_member }
      else
        format.html { render action: 'new' }
        format.json { render json: @company_member.errors, status: :unprocessable_entity }
      end
    end
  end

形式:

<%= form_for(@company_member) do |f| %>
  <!--<% if @company_member.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@company_member.errors.count, "error") %> prohibited this company_member from being saved:</h2>

      <ul>
      <% @company_member.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>-->
<%= f.fields_for @company_member do |yes| %>
email: <%= yes.text_field :email %> role: <%= yes.collection_select :company_member_role_id, CompanyMemberRole.all, :id, :name, :prompt => "Please select" %>
<% end %><br>

<%= f.fields_for @company_member do |yes| %>
email: <%= yes.text_field :email %> role: <%= yes.collection_select :company_member_role_id, CompanyMemberRole.all, :id, :name, :prompt => "Please select" %>
<% end %><br>

<%= f.fields_for @company_member do |yes| %>
email: <%= yes.text_field :email %> role: <%= yes.collection_select :company_member_role_id, CompanyMemberRole.all, :id, :name, :prompt => "Please select" %>
<% end %>

  <div class="field">
    <%= f.label :email %>
    <%= f.text_field :email %>
    <%= show_errors(@company_member, :email) %>
  </div>
  <!--<div class="field">
    <%= f.label :company_id %><br>
    <%= f.collection_select :company_id, Company.all, :id, :name %>
  </div>-->
  <div class="field">
    <%= f.label :company_member_role_id %>
    <%= f.collection_select :company_member_role_id, CompanyMemberRole.all, :id, :name, :prompt => "Please select" %>
    <%= show_errors(@company_member, :company_member_role_id) %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

它只需在数据库中保存一行,而它必须保存4行。还有一种方法可以删除未填充的行吗?

0 个答案:

没有答案