Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在控制器中访问rails哈希_Ruby On Rails_Params - Fatal编程技术网

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

Ruby on rails 如何在控制器中访问rails哈希,ruby-on-rails,params,Ruby On Rails,Params,我试图基于此创建一个逻辑: Parameters: {"combination_dose"=>{"0"=>{"a"=>"Mefenamic Acid", "b"=>"Cap", "c"=>"20"}}} 在这里,我想将其添加到我在create中使用此方法的表中: def create @combination_dose = CombinationDose.new(combination_dose_params) @shared = params[:combin

我试图基于此创建一个逻辑:

Parameters: {"combination_dose"=>{"0"=>{"a"=>"Mefenamic Acid", "b"=>"Cap", "c"=>"20"}}}
在这里,我想将其添加到我在create中使用此方法的表中:

 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"}
在表中的循环中,如下所示:

 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)

尝试允许
params.require(:combination\u dose)。允许(:generic\u id,:combinationdosename,[:a,:b,:c])