Address モデルへのポリモーフィックな関連付けを持つ User モデルがあります
class User < ActiveRecord::Base
attr_accessible :addresses, :billing_addresses, :shipping_addresses, :shipping_same_as_billing
has_many :addresses, dependent: :destroy
has_many :billing_addresses, class_name: Address, conditions: ['type = ?', :billing]
has_many :shipping_addresses, class_name: Address, conditions: ['type = ?', :shipping]
accepts_nested_attributes_for :addresses
#...
end
class Address < ActiveRecord::Base
belongs_to :user, polymorphic: true
validates_presence_of :line1, :city, :state, :zip
attr_accessible :line1, :line2, :city, :state, :zip, :type
#...
end
私のフォーム:(興味深い部分はアドレスブロックです)
= form_tag '/subscriptions', class: 'form-horizontal', id: "new_order" do |f|
-if current_user.andand.stripe_customer_id.blank?
= field_set_tag("Billing Information") do
=simple_fields_for :creditcard do |cc|
= cc.input :card_number, hint: "test cc: 4242424242424242", :wrapper => :append do
= cc.input_field :card_number, id: "card_number", class: "input-medium",
size: 20, name: ""
%div#cc_type_holder.cc_32.add-on{style:"position:absolute;width:35px"}
%div#cc_type.cc_32
= cc.input :cvc, input_html: { id: "cvc", class: "input-mini",
size: 4, name: ""}, label: "CVC", hint: "This 3 or 4-digit code is usually found on the back of your credit card"
= cc.input :expiration, as: :date, start_year: Date.today.year,
end_year: Date.today.year + 10, discard_day: true,
order: [:month, :year], input_html: { class: "input-medium", name: ""}
= simple_fields_for "user[billing_addresses]" do |billing_address|
= billing_address.hidden_field :type, input_html: { value: "billing"}
= billing_address.input :line1, input_html: { class: "input-medium"}
= billing_address.input :line2, required: false, input_html: { class: "input-medium"}
= billing_address.input :city, input_html: { class: "input-medium"}
= billing_address.input :state, input_html: { class: "input-medium"}
= billing_address.input :zip, input_html: { class: "input-medium"}
-if current_user.blank?
= field_set_tag("User Information") do
= simple_fields_for :user do |users_post|
= users_post.input :email, input_html: { value: session[:email], type: "email",
placeholder: 'user@domain.com', class: "input-medium" }
= users_post.input :password, input_html: { class: "input-medium" }
= field_set_tag("Shipping Information") do
= simple_fields_for "user[shipping_addresses]" do |shipping_address|
= shipping_address.input :shipping_same_as_billing, as: :boolean, input_html: {checked: true, id: 'shipping-same-as-billing'}
%div#shipping-form
= shipping_address.hidden_field :type, input_html: { value: "shipping"}
= shipping_address.input :line1, input_html: { class: "input-medium"}
= shipping_address.input :line2, required: false, input_html: { class: "input-medium"}
= shipping_address.input :city, input_html: { class: "input-medium"}
= shipping_address.input :state, input_html: { class: "input-medium"}
= shipping_address.input :zip, input_html: { class: "input-medium"}
#stripe_error
.form-actions
= submit_tag t(".subscription.submit"), class: 'btn btn-success'
フォームを投稿すると、次のエラーが発生します。
ActiveRecord::AssociationTypeMismatch (アドレス (#70101930814600) が必要ですが、配列 (#70101927961780) を取得しました):
変更しようとしていることに言及したスタックに関する別の投稿を読みました
config.cache_classes = true
変更はありませんでした。同じエラーが発生します。関連付けを手動で構築したくありません。ActiveRecord がこれを処理できるようで、何かが足りないだけです。
--以下のコメント用に編集
Parameters: {"utf8"=>"✓", "authenticity_token"=>"HigNtHH08ER1S6p3xMUPlreWMuHngjo5S8z74jeqsO0=", "creditcard"=>{"expiration(3i)"=>"1"}, "user"=>{"billing_addresses"=>{"type"=>"", "line1"=>"1653 Main", "line2"=>"", "city"=>"Redwood City", "state"=>"California", "zip"=>"94061"}, "email"=>"tom@tom.com", "password"=>"[FILTERED]", "shipping_addresses"=>{"shipping_same_as_billing"=>"0", "type"=>"", "line1"=>"1653 Main", "line2"=>"", "city"=>"Redwood City", "state"=>"California", "zip"=>"94061"}}, "stripeToken"=>"[FILTERED]"}
これらは私が送信しているパラメーターです。私は今、コントローラーで次のようにデバッグしています:
Rails.logger.debug params['user']['billing_addresses']
Rails.logger.debug params['user']['shipping_addresses']
u = User.new(params["user"])
Rails.logger.debug u
{"type"=>"", "line1"=>"1653 Main", "line2"=>"", "city"=>"Redwood City", "state"=>"California", "zip"=>"94061"}
{"shipping_same_as_billing"=>"0", "type"=>"", "line1"=>"1653 Main", "line2"=>"", "city"=>"Redwood City", "state"=>"California", "zip"=>"94061"}
配列がどこから来ているのかわかりません。ActiveRecord がshipping_addresses
andを取り、billing_addresses
それらをアドレスの配列として扱っていない限り...