使用 fields_for 和 accepts_nested_attributes_for 在 Rails 4 控制器中检索表单参数

Retrieving form params in Rails 4 controller using fields_for and accepts_nested_attributes_for

我敢肯定这是最愚蠢的问题,但我无法理解它。

我在一个简单的 has_one/belongs_to 关系中有两个模型

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

在我的表格中,我正在使用 fields_for.

收集两个模型的信息

_form.html.erb(截断示例)

<%= form_for @registration_code, :html => {:class => "form", role: "form"} do |f| %>

  <%= f.label :registration_code, "Registration Code", :class => "control-label" %>
  <%= f.text_field :registration_code %>

  <%= f.fields_for @billing_transaction do |bt|  %>
    <%= bt.label :transaction_amount, "Transaction Amount", :class => "control-label" %>
    <%= bt.number_field :transaction_amount %>
  <% end %>

<% end %>

在我的控制器中,我有以下内容。

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 # DOES THIS HAVE TO TAKE PARAMS?
    @billing_transaction.registration_code = @registration_code # DO I NEED THIS LINE?

    ##### THE TROUBLE IS ON THIS NEXT LINE #####
    @billing_transaction.transaction_amount = params[:billing_transaction_attributes][:transaction_amount] # THIS NEVER GETS SET!  NOT SURE HOW TO ACCESS THE PARAMS

    respond_to do |format|
      if @registration_code.save && @billing_transaction.save
        format.html { redirect_to registration_codes_path, notice: 'Registration code was successfully created.' }
      else
        format.html { render :new }
        format.json { render json: @customer.errors, status: :unprocessable_entity }
      end
    end
end

private

# Never trust parameters from the scary internet, only allow the white list through.
def registration_code_params
    params.require(:registration_code).permit(:registration_code, :expires_at, billing_transaction_attributes: [:transaction_amount])
end

参数已提交,我可以正常访问主要模型 (registration_code) 的参数。但我终其一生都无法弄清楚如何获取 "sub" 模型 (billing_transaction) 的参数并在控制器中使用它们。

{"utf8"=>"✓",
"authenticity_token"=>"ePpqwJkeTAGMzb5WRWeI6aYCx4xpqvq4rl2m405IbwLdbp9xE0RyPgTZ6NmX8SvCFu94GKrfMfV9PrOkKa1BLg==", 
"registration_code"=>{"registration_code"=>"HFmkbQEN",
"expires_at"=>"2015-07-16", 
"billing_transaction"=>{"transaction_amount"=>"958.40" }}, 
"commit"=>"Create Registration Code"}

以访问billing_transaction.transaction_amount为例,我尝试了很多变体:

无论我输入什么,我似乎都无法访问那个嵌套的参数数组。

求助。现在感觉超级傻。谢谢。

检索计费交易的哈希值:

registration_code_params[:billing_transaction_attributes]

检索散列中的第一个(也是唯一一个)key/value 对:

key, value = registration_code_params[:billing_transaction_attributes].first
puts key     #transaction_amount
puts value   #958.40

如下对您的 newcreate 方法进行一些重要更改将解决您的问题。

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 do |format|
    if @registration_code.save
      format.html { redirect_to registration_codes_path, notice: 'Registration code was successfully created.' }
    else
      format.html { render :new }
      format.json { render json: @customer.errors, status: :unprocessable_entity }
    end
  end
end

这就是 nested_attributesDB.

中的保存方式