使用fields_for和accepts_nested_attributes_for

时间:2015-07-11 02:06:31

标签: ruby-on-rails nested-attributes

我很肯定这是最愚蠢的问题,但我无法绕过它。

我在简单的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收集BOTH模型的信息。

_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

提交参数并且我可以访问主模型的params(registration_code)就好了。但我不能为我的生活弄清楚如何获取“子”模型(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,我尝试了很多变体:

  • PARAMS [:billing_transaction_attributes] [:TRANSACTION_AMOUNT]
  • PARAMS [:billing_transaction] [:TRANSACTION_AMOUNT]
  • PARAMS [@billing_transaction] [:TRANSACTION_AMOUNT]
  • PARAMS [:registration_code] [:billing_transaction] [:TRANSACTION_AMOUNT]

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

帮助。现在感觉超级愚蠢。感谢。

2 个答案:

答案 0 :(得分:1)

以下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_attributes保存在 数据库 中的方式。

答案 1 :(得分:0)

检索结算交易的哈希值:

registration_code_params[:billing_transaction_attributes]

检索哈希中的第一个(也是唯一的)键/值对:

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