我有以下模特关系:
OrderModel:
has_one :credit_card
accepts_nested_attributes_for :credit_card
attr_accessible :user_id, :date_updated, :date_finished, :amount, :payment_method, :status, :billing_cycle, :auth_net_subscription_id, :billing_start_date, :credit_card_attributes, :billing_address_id, :cc_id
CreditCardModel:
belongs_to :order
这是我的订单管理员(订单#结账)
def checkout
@order = current_order
@cc = CreditCard.new
@order.build_credit_card
respond_with @order
end
以下是在订单上输入CC的表格:
<%= form_for(@order, :url => finish_checkout_path, :html => { :class => 'validate' }) do |f| %>
<%= f.fields_for @cc do |cc| %>
<%= cc.text_field :cc_number, :placeholder => "Credit Card Number", :class => "full-width validate[required, creditCard] cc" %>
<%= cc.text_field :name, :placeholder => "Name as it appears on card", :class => "full-width validate[required]" %>
<%= select_month(Date.today, {:field_name => 'exp_month', :prefix => "order[credit_card]", :prompt => "EXP. MONTH"}, { :class => "dk half-width validate[required,past] marginRight10" }) %>
<%= select_year(Date.today, {:field_name => 'exp_year', :prefix => "order[credit_card]", :prompt => "EXP. YEAR", :start_year => Date.today.year, :end_year => Date.today.year + 10}, { :class => "dk half-width validate[required]" }) %>
<%= link_to "What's this?", "#", :class => 'cvv-help' %>
<%= cc.text_field :cvv, :class => 'half-width validate[required] marginRight10', :placeholder => "CVV" %>
<%= cc.text_field :zip_code, :class => 'half-width validate[required]', :placeholder => "Zip Code" %>
<% end %>
<%= f.hidden_field :amount, :value => @order.products.collect(&:price).reduce(&:+) %>
<p class="tos">By clicking the button below you agree to our <a href="#" class="pink">terms of service</a>.</p>
<p class="align-center"><%= f.submit "Submit", :class => 'btn submit' %></p>
<% end %>
这是我更新订单的地方(订单#finished):
current_order.update_attributes(params[:order])
当我这样做时,我收到以下错误:Can't mass-assign protected attributes: credit_card
我在attr_accessible
中明确拥有credit_card_attributes,所以我不确定为什么会出错。
答案 0 :(得分:1)
不确定,但可能是因为您的控制器代码:
def checkout
@order = current_order
@cc = CreditCard.new
@order.build_credit_card
respond_with @order
end
这样,您在fields_for中使用的信用卡就不会与您的订单相关联。这可能是问题所在。
尝试这样做:
def checkout
@order = current_order
@order.build_credit_card
respond_with @order
end
和
<%= f.fields_for :credit_card do |cc| %>