2016-04-18 10 views
0

Ich versuche, eine Logik auf dieser Basis zu erstellen:Wie Schienen für den Zugriff auf Hashes in Controller

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

Hier habe ich dies für die in Tabelle in Ich bin mit dieser Methode hinzufügen möchten erstellen:

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 

Ende

Und das ist die params:

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

Ich möchte diese hinzufügen: wie diese

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

in der Tabelle in einer Schleife:

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

versuchen 'params.require erlaubt (: combination_dose) .permit (: generische_id,: kombinationsname, [: a,: b,: c]) ' – Nithin

Antwort

0

Lösung für diese Frage lautet:

@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) 
Verwandte Themen