带有fields_for的Rails 3质量分配错误 [英] Rails 3 Mass-Assignment Errors with fields_for

查看:76
本文介绍了带有fields_for的Rails 3质量分配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下模型关系:
OrderModel:

I have the following model relationships:
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:

CreditCardModel:

belongs_to :order

这是我的订单控制器(orders#checkout)

Here is my Order Controller (orders#checkout)

def checkout
  @order = current_order
  @cc = CreditCard.new
  @order.build_credit_card
  respond_with @order
end

以下是用于在订单上输入抄送的表格:

Here is the form for entering in a CC on the order:

<%= 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 %>

这是我更新订单的地方(orders#finish):

And here is where I update the order (orders#finish):

current_order.update_attributes(params[:order])

执行此操作时,出现以下错误:Can't mass-assign protected attributes: credit_card

When I do this, I get the following error: Can't mass-assign protected attributes: credit_card

我的attr_accessible中显然有credit_card_attributes,所以我不确定为什么会出错.

I clearly have the credit_card_attributes in my attr_accessibleso I am not sure why this is erroring out.

推荐答案

不确定,但这可能是由于您的控制器代码造成的:

Not sure, but it might be because of your controller code:

def checkout
   @order = current_order
   @cc = CreditCard.new
   @order.build_credit_card
   respond_with @order
end

这样,您在fields_for中使用的信用卡就不会链接到您的订单.这可能是问题所在.

With this, the credit card that you use in your fields_for is not linked to your order. That might be the problem.

尝试这样做:

def checkout
   @order = current_order
   @order.build_credit_card
   respond_with @order
end

<%= f.fields_for :credit_card do |cc| %>

这篇关于带有fields_for的Rails 3质量分配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆