如何从控制器的方法设置在导轨4的嵌套参数的值?(How to set values of neste

2019-10-22 20:39发布

我有请求模型和嵌套模型filled_cartridges当创建新的请求对象我还需要设置嵌套则params的值中创建控制方法。 我有很多嵌套PARAMS,所以我需要通过他们迭代,这样的事情:

params[:request][:filled_cartridges_attributes].each do |_,value|
  # here i try to set :client_id parameter, which is nested
  # where @client.id is defined
    value[:client_id] = @client.id # i am pretty sure that the problem is here
  # is it a correct way of ding that?

end

编辑:

 has_many :filled_cartridges, inverse_of: :request, dependent: :destroy accepts_nested_attributes_for :filled_cartridges, reject_if: proc { |attributes| attributes['title'].blank? },allow_destroy: true 

我的嵌套模式:

  create_table :filled_cartridges do |t| t.integer :client_id, null: false t.string :cartridge_name, null: false t.integer :cartridge_id, null: false t.integer :request_id, null: false t.integer :count, default: 1 t.datetime :fill_date t.timestamps null: false end 

这里CLIENT_ID,REQUEST_ID和cartridge_id都应该应内部控制设定。 和我的强烈PARAMS:

 def request_params params.require(:request).permit(:name, :address, :phone, :mobile,:date, :filled_cartridges_attributes => [:client_id,:cartridge_name,:cartridge_id, :request_id,:count,:fill_date,:_destroy,:id], end 

文章来源: How to set values of nested parameters in Rails 4 from controller method?