How to access nested params

前端 未结 1 712
再見小時候
再見小時候 2020-12-31 10:09

I would like to get some nested params. I have an Order that has many Items and these Items each have a Type. i would like to get the type_id parameter from the controllers

相关标签:
1条回答
  • 2020-12-31 10:45

    To access the nested fields from params do the following:

    params[:order][:items_attributes].values.each do |item|
      item[:type_id]
    end if params[:order] and params[:order][:items_attributes]
    

    Above solution will work ONLY if you have declared the correct associations and accepts_nested_attributes_for.

    class Order < ActiveRecord::Base
      has_many :items
      accepts_nested_attributes_for :items, :allow_destroy => true
    end
    
    class Item < ActiveRecord::Base
      belongs_to :order
    end
    
    0 讨论(0)
提交回复
热议问题