如何将数据存储在具有多个键和值的 hstore 数据类型中 rails 5
How to store data in hstore data type with multiple keys and values rails 5
views/_form.html.erb
Extra fee field name : <%= text_field_tag :extra_fee_field,nil, placeholder: 'Enter field name',name: "payment_plan[payment_fees[extra_fee_field]][]" %>fees
Extra fee value : <%= text_field_tag :extra_fee_value,nil, placeholder: 'Enter fee value in %',name: "payment_plan[payment_fees[extra_fee_value]][]" %> %
<%= submit_tag "Add",id: "add" %>
controllers/payment_plan_controller.rb
def create
@payment_plan = PaymentPlan.new(payment_plan_params)
@payment_plan.save
结束
private
def payment_plan_params
params.require(:payment_plan).permit(:pay_within_days,:display_active,
:payment_fees=>[:extra_fee_field=>[],:extra_fee_value=>[]])
end
model/payment_plan.rb
class PaymentPlan < ApplicationRecord
store_accessor :payment_fees
end
以下格式的数据存储:-
payment_fees: {"extra_fee_field"=>"[\"serfere\", \"sfsdfdff\"]", "extra_fee_value"=>"[\"5\ ", \"7\"]"}, display_active: false, created_at: "2016-07-12 06:41:48", updated_at: "2016-07-12 06:41:48">
请帮帮我。
提前致谢..:)
在尽了最大努力后,我得到了解决方案,上面所有代码都是正确的,只是改变了控制器的方法
@extra_fee_field = params[:payment_plan][:payment_fees][:extra_fee_field]
@extra_fee_value = params[:payment_plan][:payment_fees][:extra_fee_value]
@payment_plan[:payment_fees] = @extra_fee_field.zip(@extra_fee_value).to_h
之后我可以将数据存储在 hstore 数据类型中
views/_form.html.erb
Extra fee field name : <%= text_field_tag :extra_fee_field,nil, placeholder: 'Enter field name',name: "payment_plan[payment_fees[extra_fee_field]][]" %>fees
Extra fee value : <%= text_field_tag :extra_fee_value,nil, placeholder: 'Enter fee value in %',name: "payment_plan[payment_fees[extra_fee_value]][]" %> %
<%= submit_tag "Add",id: "add" %>
controllers/payment_plan_controller.rb
def create
@payment_plan = PaymentPlan.new(payment_plan_params)
@payment_plan.save
结束
private
def payment_plan_params
params.require(:payment_plan).permit(:pay_within_days,:display_active,
:payment_fees=>[:extra_fee_field=>[],:extra_fee_value=>[]])
end
model/payment_plan.rb
class PaymentPlan < ApplicationRecord
store_accessor :payment_fees
end
以下格式的数据存储:-
payment_fees: {"extra_fee_field"=>"[\"serfere\", \"sfsdfdff\"]", "extra_fee_value"=>"[\"5\ ", \"7\"]"}, display_active: false, created_at: "2016-07-12 06:41:48", updated_at: "2016-07-12 06:41:48">
请帮帮我。 提前致谢..:)
在尽了最大努力后,我得到了解决方案,上面所有代码都是正确的,只是改变了控制器的方法
@extra_fee_field = params[:payment_plan][:payment_fees][:extra_fee_field]
@extra_fee_value = params[:payment_plan][:payment_fees][:extra_fee_value]
@payment_plan[:payment_fees] = @extra_fee_field.zip(@extra_fee_value).to_h
之后我可以将数据存储在 hstore 数据类型中