如何在Ruby on Rails中存储数组的数组值

时间:2014-09-17 13:36:34

标签: ruby-on-rails ruby

我想保存数据库中数组中的所有值。当我提交表单时,我在终端上收到以下消息:

Started POST "/admin/demands" for 127.0.0.1 at 2014-09-17 10:22:07 -0300
Processing by Admin::DemandsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"4MNZJOVeeMmMU7DyrL4J4Cu8kIzY8EDKMtAoDBM6svM=", "commit"=>"Criar", "demand"=>{"item"=>["055"], "product_id"=>["1"], "collection_id"=>["1"], "color_id"=>["1"], "quantity"=>["3"], "width"=>["3"], "height"=>["3"], "model_id"=>["1"], "completion_id"=>["1"], "system"=>["dgb"]}}
  User Load (0.2ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 2  ORDER BY `users`.`id` ASC LIMIT 1
Unpermitted parameters: item, product_id, collection_id, color_id, quantity, width, height, model_id, completion_id, system
   (0.2ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `demands` (`created_at`, `updated_at`) VALUES ('2014-09-17 13:22:07', '2014-09-17 13:22:07')
   (61.3ms)  COMMIT
Redirected to http://localhost:3000/admin/demands/new
Completed 302 Found in 121ms (ActiveRecord: 65.9ms)

我的控制器是:

  def create
    @demand = Demand.new(demand_params)
    authorize @demand

    respond_to do |format|
      if @demand.save
        format.html { redirect_to new_admin_demand_url, notice: 'Demand was successfully created.' }
        format.json { render :show, status: :created, location: @demand }
      end
    end
  end

  def demand_params
    params.require(:demand).permit(:item, :product_id, :collection_id, :color_id, :model_id, :completion_id, :system, :width, :height, :quantity)
  end

为什么显示“未经许可的参数”?如何存储数组的所有值?

EDITED

我从表单中获取所有输入都是从javascript代码设置的值。当用户单击“添加项目”时,其中一个事件是这样的附加:

$(document).ready(function($) {
  $("button#add_item").click(function() {

    var item = $('input#item_demand').val(),
      product = $("select#product_demand option").filter(':selected').val(),
      collection = $("select#collection_demand option").filter(':selected').val(),
      color = $("select#color_demand option").filter(':selected').val(),
      quantity = $("input#quantity_demand").val(),
      width = $("input#width_demand").val(),
      height = $("input#height_demand").val(),
      model = $("select#model_demand option").filter(':selected').val(),
      completion = $("select#completion_demand option").filter(':selected').val(),
      system = $("input#system_demand").val();

      $("#hidden_inputs form").append(
        "<input class='hidden' type='hidden' id='demand_item' name='demand[item][]' value='" + item + "'>" +
        "<input class='hidden' type='hidden' id='demand_product_id' name='demand[product_id][]' value='" + product + "'>" +
        "<input class='hidden' type='hidden' id='demand_collection_id' name='demand[collection_id][]' value='" + collection + "'>" +
        "<input class='hidden' type='hidden' id='demand_color_id' name='demand[color_id][]' value='" + color + "'>" +
        "<input class='hidden' type='hidden' id='demand_quantity' name='demand[quantity][]' value='" + quantity + "'>" +
        "<input class='hidden' type='hidden' id='demand_width' name='demand[width][]' value='" + width + "'>" +
        "<input class='hidden' type='hidden' id='demand_height' name='demand[height][]' value='" + height + "'>" +
        "<input class='hidden' type='hidden' id='demand_model_id' name='demand[model_id][]' value='" + model + "'>" +
        "<input class='hidden' type='hidden' id='demand_completion_id' name='demand[completion_id][]' value='" + completion + "'>" +
        "<input class='hidden' type='hidden' id='demand_system' name='demand[system][]' value='" + system + "'>"
      );
  });
});

在视图中,我有这个:

  <button type="button" id="add_item" class="btn btn-primary btn-lg btn-block ">Adicionar item</button>

  <div id="hidden_inputs">
    <%= simple_form_for([:admin, @demand], :html => {:multipart => true}) do |f| %>
      <%= f.button :submit %>
    <% end %>
  </div>

已编辑2

伙计们,我需要的就是这样:

Table (Demands)

    id    item    product_id    collection_id    color_id    quantity_id   width    height     model_id    completion_id    system
    1     001     1             1                1           1             5        5          1           1                dfg
    2     055     3             2                4           2             4        6          3           2                kfg
...

谢谢大家。

0 个答案:

没有答案