gpt4 book ai didi

ruby-on-rails - 选择 :multiple => true not saving values

转载 作者:行者123 更新时间:2023-12-04 14:01:05 24 4
gpt4 key购买 nike

我试图从由 rails helper select 生成的多选字段中保存多个选定的值。

  <div class="form-group">
<%= f.label :available_type, "Available in category" %><br>
<%= f.select :available_type, options_for_select(Setting.subscription_type, @terminal.available_type), { }, { class: "form-control", :multiple => true, :size => 5 } %>
</div>

这呈现如下(所选值来自之前的尝试,没有完美工作的“:multiple => true”属性):
<select class="form-control" id="terminal_available_type" multiple="multiple" name="terminal[available_type][]" size="5">
<option value="Postpaid">Postpaid</option>
<option value="MBB">MBB</option>
<option selected="selected" value="Prepaid">Prepaid</option>
</select>

enter image description here

任何帮助表示赞赏。 :)

编辑:
我试过把 serialize :available_type在我的终端模型中,没有改变任何东西。 :-/

编辑 2:
我注意到多个选择的字段在我标记选项时没有将选项标记为已选择。如果我手动添加 Selected 属性,我会得到这些参数:
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"RrwWlKk8XlGeC+dTu/w6oSM68e9LcbUFJWTI+eRS9mI=", "terminal"=>{"inndate"=>"2015-01-13", "outdate"=>"", "brand_id"=>"2", "name"=>"iPhone 5c", "available_type"=>["", "MBB", "Prepaid"], "product_number"=>"3r2342", "ean_code"=>"", "navision_nb"=>"324234", "cost_price_map"=>"3200.0", "manual_price"=>"", "sales_info"=>"Just sell!"}, "commit"=>"Submit", "action"=>"update", "controller"=>"terminals", "id"=>"2"}

available_type 字段的值为 "available_type"=>["", "MBB", "Prepaid"]
我使用 rails 4.0.2,这里是我的强参数:
# Never trust parameters from the scary internet, only allow the white list through.
def terminal_params
params.require(:terminal).permit(:inndate, :outdate, :brand_id, :name, :product_number, :navision_nb, :cost_price_map, :manual_price, :sales_info, :available_type)
end

最佳答案

终于找到答案了!

这个问题是 PG 和 Rails 4 的组合。

首先,我需要将列从字符串转换为标记为数组的文本列,如下所示:

class ChangeAvailableTypeOnTerminals < ActiveRecord::Migration
def up
change_column :terminals, :available_type, :text, array: true, default: []
end

def down
change_column :terminals, :available_type, :string
end
end

然后我需要将强参数视为终端 Controller 中的数组,如下所示:
# Never trust parameters from the scary internet, only allow the white list through.
def terminal_params
params.require(:terminal).permit(:inndate, :outdate, {:available_type => []}, :brand_id, :name, :product_number, :navision_nb, :cost_price_map, :manual_price, :sales_info)
end

更具体地说:

更改自 :available_type{:available_type => []}
这为我解决了这个问题。 :)

关于ruby-on-rails - 选择 :multiple => true not saving values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20968665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com