如何在控制器中访问 rails 哈希

How to access rails hashes in controller

我正在尝试基于此创建逻辑:

Parameters: {"combination_dose"=>{"0"=>{"a"=>"Mefenamic Acid", "b"=>"Cap", "c"=>"20"}}}

这里我想把它添加到 table 因为我在创建中使用了这个方法 :

 def create
@combination_dose = CombinationDose.new(combination_dose_params)
 @shared = params[:combination_dose]
respond_to do |format|
# puts @shared.first
  # @shared.each do |p|
    @shared.each_with_index do |(key, value), index|
     value.each_with_index do |(key, value), index|
        print "key: #{key}, value: #{value}, index: #{index}\n"
        # cc = CombinationContent.create(doseunit_id: "David",  generic_strength_content: "Code Artist", combination_generic: "cg")
     end
      end

  # end
  format.html { render json: @shared }
  #  format.html { render json: @shared  }
  # if @combination_dose.save
  #   format.html { render json: @shared }
  #   format.json { render :show, status: :created, location: @combination_dose }
  # else
  #   format.html { render :new }
  #   format.json { render json: @combination_dose.errors, status: :unprocessable_entity }
  # end
end

结束

这是参数:

  def combination_dose_params
  params.require(:combination_dose).permit(:generic_id, :combinationdosename)
end

我要添加这些:

{"a"=>"Mefenamic Acid", "b"=>"Cap", "c"=>"20"}

在 table 的循环中,像这样:

 doseunit_id: Mefenamic Acid
 generic_strength_content:Cap
 combination_generic: "cg"

这个问题的答案是:

  @shared.each_with_index do |(key, value), index|
  @cc = CombinationContent.new(doseunit_id: value[:a],  generic_strength_content: value[:b], combination_generic: value[:c])
  @cc.save(validate: false)