nested attributes - Retrieving form params in Rails 4 controller using fields_for and accepts_nested_attributes_for -
i positive dumbest question cannot wrap head around it.
i have 2 models in simple has_one/belongs_to relationship
registration_code.rb
class registrationcode < activerecord::base has_one :billing_transaction accepts_nested_attributes_for :billing_transaction
billing_transaction.rb
class billingtransaction < activerecord::base belongs_to :registration_code
in form collecting information both models using fields_for.
_form.html.erb (truncated example)
<%= form_for @registration_code, :html => {:class => "form", role: "form"} |f| %> <%= f.label :registration_code, "registration code", :class => "control-label" %> <%= f.text_field :registration_code %> <%= f.fields_for @billing_transaction |bt| %> <%= bt.label :transaction_amount, "transaction amount", :class => "control-label" %> <%= bt.number_field :transaction_amount %> <% end %> <% end %>
in controller, have following.
registration_code_controller.rb
def new @registration_code = registrationcode.new @billing_transaction = billingtransaction.new @billing_transaction.registration_code = @registration_code end def create @registration_code = registrationcode.new(registration_code_params) @billing_transaction = billingtransaction.new # have take params? @billing_transaction.registration_code = @registration_code # need line? ##### trouble on next line ##### @billing_transaction.transaction_amount = params[:billing_transaction_attributes][:transaction_amount] # never gets set! not sure how access params respond_to |format| if @registration_code.save && @billing_transaction.save format.html { redirect_to registration_codes_path, notice: 'registration code created.' } else format.html { render :new } format.json { render json: @customer.errors, status: :unprocessable_entity } end end end private # never trust parameters scary internet, allow white list through. def registration_code_params params.require(:registration_code).permit(:registration_code, :expires_at, billing_transaction_attributes: [:transaction_amount]) end
the params submitted , can access params primary model (registration_code) fine. cannot life of me figure out how parameters of "sub" model (billing_transaction) , use them in controller.
{"utf8"=>"✓", "authenticity_token"=>"eppqwjketagmzb5wrwei6aycx4xpqvq4rl2m405ibwldbp9xe0rypgtz6nmx8svcfu94gkrfmfv9prokka1blg==", "registration_code"=>{"registration_code"=>"hfmkbqen", "expires_at"=>"2015-07-16", "billing_transaction"=>{"transaction_amount"=>"958.40" }}, "commit"=>"create registration code"}
to access billing_transaction.transaction_amount, example, have tried many variations:
- params[:billing_transaction_attributes][:transaction_amount]
- params[:billing_transaction][:transaction_amount]
- params[@billing_transaction][:transaction_amount]
- params[:registration_code][:billing_transaction][:transaction_amount]
no matter enter cannot seem access nested array of parameters.
help. feeling super dumb right now. thanks.
a few important changes new
, create
methods below solve problem.
def new @registration_code = registrationcode.new @billing_transaction = @registration_code.build_billing_transaction end def create @registration_code = registrationcode.new(registration_code_params) respond_to |format| if @registration_code.save format.html { redirect_to registration_codes_path, notice: 'registration code created.' } else format.html { render :new } format.json { render json: @customer.errors, status: :unprocessable_entity } end end end
this how nested_attributes
gets saved in db.
Comments
Post a Comment