【问题标题】:Number_field_tag to create unique records for each item/ quantityNumber_field_tag 为每个项目/数量创建唯一记录
【发布时间】:2014-06-19 17:55:27
【问题描述】:

我使用number_field_tag 来允许用户指明他们拥有多少给定项目。然后,对于这些项目中的每一个,应该在模型中创建一个新记录。所有项目字段的默认数量为 0。

我对如何让参数正确传递有点困惑(即,如何编写强参数和表单代码)。我编写的控制器代码假定我会得到一个如下所示的哈希:

itemrecord = {
  "tent" => 1
  "sleeping bag" => 0
  ...
}

然后这样,我可以使用以下create 代码(其中:item_name 是模型Inventory 的属性)进行记录:

items_to_be_saved = []
   inventory_params.each do |item, quantity|
      quantity.times do
         items_to_be_saved << ( :item_name => item )
      end
   end

目前,我下面的视图/强参数代码正在生成以下输出,这与我需要的不同,:item_record 是一个数量数组,没有选择相关项目。

Parameters: {"utf8"=>"✓", "authenticity_token"=>"m2NMruoFRr6lpsuVMK9UthlY0bsJsPmf1LWce2uKaH4=", ":itemrecord"=>["", "", "", "", "", "", "", "", "2", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""], "commit"=>"Go!", "method"=>"post"}

表格代码

          <%= form_tag inventories_path method: :post do %>

            <div class="col-xs-12">
              <p><b>Items people are asking for</b></p>
            </div>

            <% @wishlist.each do |category, list| %>
              <div class="col-xs-2">
                <div class="form-group box">
                  <h5> <%="#{category}"%> </h5>
                    <% list.each do |thing| %>
                        <%= number_field_tag ":itemrecord[]", "#{thing}", {:size => 1, :placeholder => '0', :min => 0} %>
                        <%= label_tag "#{thing}" %>
                      </br>
                    <% end %>
                </div>
              </div>  
            <% end %>

              <%= submit_tag "Go!", class: "btn btn-primary btn-large btn-block" %>
            </div>
          <% end %>

控制器代码

def inventory_params
  params.require(":itemrecord")
end

编辑!我有办法!但是其中的params 部分非常hacky,需要更新。这是有效的代码:

查看

<%= number_field_tag "#{thing}", :quantity, min: 0, placeholder: 0 %>

控制器

def create

items_to_be_saved = []
inventory_params.each do |item, quantity|
  quantity = quantity.to_i
  quantity.times do
    items_to_be_saved << ({:signup_id => @signup_parent.id, :item_name => item })
  end
end

if Inventory.create items_to_be_saved
    flash[:success] = "Thanks!"
    redirect_to root_path
else
    render new_inventory_path
end

end

def inventory_params
    params.except(:action, :controller, :method, :commit, :utf8, :authenticity_token)
end

问题在于params.except 违背了“强”参数的目的。我基本上需要将参数传递为控制器定义的项目清单中的任何一个。与表单类似,控制器代码为

def wishlist
    @wishlist = {
        "Category1" => ["ItemA", "ItemB"],
        "Category2" => ["ItemC", "ItemD"],
    }
end

正如您在视图中看到的,ItemAItemBItemCItemD 是生成的每个表单字段的名称,以及 params 哈希的键(:quantity 是价值)。问题是如何允许它。下面的错误不起作用:undefined method[]' for nil:NilClasson thepermit` 行

def inventory_params
  @wishlist.each do |category, list|
    list.each do |thing|
      params.permit[thing][:quantity]
      params.reject { |k, v| v == "" }
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails forms parameters strong-parameters form-helpers


    【解决方案1】:

    好的,我解决了。我意识到这真的很简单......当你工作太久时会发生这种情况......大脑会停止

    控制器代码:

    def create
    
        items_to_be_saved = []
        inventory_params.each do |item, quantity|
          quantity = quantity.to_i
          quantity.times do
          items_to_be_saved << (:item_name => item )
        end
    end
    
    if Inventory.create items_to_be_saved
        flash[:success] = "Thanks!"
        redirect_to root_path
    else
        render new_inventory_path
    end
    
    end
    
    def inventory_params
        params.permit[:quantity]
        params.reject { |k, v| v == "" }
    end
    

    查看代码:

              <%= form_tag inventories_path method: :post do %>
    
                <div class="col-xs-12">
                  <p><b>Items people are asking for</b></p>
                </div>
    
                <% @wishlist.each do |category, list| %>
                  <div class="col-xs-2">
                    <div class="form-group box">
                      <h5> <%="#{category}"%> </h5>
                        <% list.each do |thing| %>
                            <%= number_field_tag "#{thing}", :quantity, min: 0, placeholder: 0 %>
                            <%= label_tag "#{thing}" %>
                          </br>
                        <% end %>
                    </div>
                  </div>  
                <% end %>
    
                  <%= submit_tag "Go!", class: "btn btn-primary btn-large btn-block" %>
                </div>
    
              <% end %>
    

    【讨论】:

      猜你喜欢
      • 2015-08-23
      • 2014-09-30
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2022-11-25
      • 1970-01-01
      相关资源
      最近更新 更多