视图/ _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" %>
控制器/ 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
模型/ payment_plan.rb
class PaymentPlan < ApplicationRecord
store_accessor :payment_fees
end
以下格式存储数据: -
payment_fees:{“extra_fee_field”=&gt;“[\”serfere \“,\”sfsdfdff \“]”,“extra_fee_value”=&gt;“[\”5 \“,\”7 \“]”} ,display_active:false,created_at:“2016-07-12 06:41:48”,updated_at:“2016-07-12 06:41:48”&gt;
请帮帮我。 在此先感谢.. :))
答案 0 :(得分:0)
@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数据类型
中