一次提交即可进行多次保存

时间:2016-10-31 04:05:01

标签: ruby-on-rails multiple-forms

我需要一个包含1个单选按钮,1个提交按钮和每个列出项目的复选框的表单。

提交后,表单应为每个选中的项目保存单独的记录。每个保存的项目都应包含单选按钮的值以及其他隐藏值。

表单呈现但在提交时崩溃。错误消息是:

undefined method `permit' for #<Array:0x007fb4b3b1a520>

我的代码是:

     <%= form_tag(controller: "handoffs", action: "create", method: "post") %>
            <%= radio_button_tag(:attend, "arrive") %>
            <%= label(:handoff_arrive, "drop-off") %>
            <%= radio_button_tag(:attend, "depart") %>
            <%= label(:handoff_depart, "pick-up") %>

            <% @parent.children.each do |child| %>
                <%= check_box_tag "handoff[][check]" %>
                <strong>
                <%= child.fname %>
                <%= child.mname %>
                <%= child.lname %>
                </strong><br>

                <% group = Group.find(child.group_id) %>

                <%= hidden_field_tag "handoff[][attend]" %>
                <%= hidden_field_tag "handoff[][group_name]", :value => group.name %>
                <%= hidden_field_tag "handoff[][child_id]", :value => child.id %>
                <%= hidden_field_tag "handoff[][center_id]", :value => @center.id %>
                <%= hidden_field_tag "handoff[][escort_fname]", :value => @parent.fname %>
                <%= hidden_field_tag "handoff[][escort_lname]", :value => @parent.lname %>
                <%= hidden_field_tag "handoff[][child_fname]", :value => child.fname %>
                <%= hidden_field_tag "handoff[][child_mname]", :value => child.mname %>
                <%= hidden_field_tag "handoff[][child_lname]", :value => child.lname %>
            <% end %>
            <%= button_to :submit, :class => 'f_submit' %>
        <% end %>

控制器操作:

def new
    @handoff = Handoff.new
    @parent = current_parent
    @center = Center.find(@parent.center_id)
  end

  def create
    params["handoff"].each do |handoff|
      if params[:handoff["check"]] != ""
      @handoff = Handoff.new(handoff_params)
      @handoff.save
      end
    end
  end

  def handoff_params

    params.require(:handoff).permit(:attend, :group_name, :child_id, :center_id, :escort_fname, :escort_lname, :child_fname, :child_mname, :child_lname)
  end

请求参数(错误报告中)

{"utf8"=>"✓",
"authenticity_token"=>"snqrS130bXNV4bmMHOMlXeyhX2rWFVpmY/PYIv0jn97MBOLSWWw2jBbeYGPyjt7O9l5pRVNuFiu1qOwkGpELTA==",
"attend"=>"depart", "handoff"=>[{"check"=>"1", "attend"=>"",
"group_name"=>"{:value=>\"Gifted\"}", "child_id"=>"{:value=>60}",
"center_id"=>"{:value=>4}", "escort_fname"=>"{:value=>\"Richard\"}",
"escort_lname"=>"{:value=>\"Messing\"}",
"child_fname"=>"{:value=>\"Aaron\"}",
"child_mname"=>"{:value=>\"Lawrence\"}",
"child_lname"=>"{:value=>\"Schwartz\"}"}, {"check"=>"1", "attend"=>"",
"group_name"=>"{:value=>\"Arts & Crafts\"}",
"child_id"=>"{:value=>61}", "center_id"=>"{:value=>4}",
"escort_fname"=>"{:value=>\"Richard\"}",
"escort_lname"=>"{:value=>\"Messing\"}",
"child_fname"=>"{:value=>\"Joseph\"}",
"child_mname"=>"{:value=>\"Michael\"}",
"child_lname"=>"{:value=>\"Messing\"}"}], "method"=>"post",
"controller"=>"handoffs", "action"=>"create"}

1 个答案:

答案 0 :(得分:0)

我有类似的情况。首先,您需要创建包含您要导出的所有值的数组我使用帮助器来执行此操作。我会告诉你我的例子:

def get_table_of_keywords(keywords)
    exact_keywords = []
    keyword_array.each do |key|
      if !key.blank?
        exact_keywords << {keyword: key, created_at: DateTime.now.in_time_zone, updated_at: DateTime.now.in_time_zone }
      end
    end
  exact_keywords
  end

您正在使用创建

@inserted_keywords = @campaign.keywords.create(get_table_of_keywords(params[:keyword][:keyword])) # add new keywords to the list