gpt4 book ai didi

ruby-on-rails - Rails 3 中的 fields_for 问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:08 25 4
gpt4 key购买 nike

将另一个应用程序从 Rails 2 转换为 Rails 3 - 在我的一个观点中,我有以下代码:

<%fields_for "order[commission_attributes][]", commission do |f|%>

这是由下面的 Controller 操作驱动的:

def edit
@order = Order.find(params[:id],:include=>[{:orderitems=>:item_schedules}, :memberids ],:order=>"orderitems.updated_at DESC")
@active_order_iems = []
@inactive_order_items = []
@order.orderitems.each do |oi|
oi_active = nil
oi.item_schedules.each do |is|
if (is.end_date > Date.today)
oi_active =true
break
end
end
@active_order_iems << oi if !oi_active.nil?
@inactive_order_items << oi if oi_active.nil?
end

@commissions = @order.commissions.find(:all)
@ordertypes = Ordertype.find(:all, :order=>"description")
@salesreps = Salesrep.find(:all, :order=>"fullname")
@customers = Customer.find(:all, :order=>"company_name")
@memberids = Memberid.find_all_by_customer_id(@order.customer_id)
@products = Product.find(:all, :include=>[:items],:order=>["products.description"])
@partners = Partner.find(:all, :order=>"company_name")
end

def get_reps
@salesreps = Salesrep.find(:all,:order=>"fullname")
@commission = Commission.new
render :partial=>'commission'
end

在我的“订单”模型中,我有这个:

 def commission_attributes=(commission_attributes)
commission_attributes.each do |attributes|
if attributes[:id].blank?
commissions.build(attributes)
else
commission = commissions.detect{|t| t.id == attributes[:id].to_i}
commission.attributes = attributes
end
end
end

这在 rails 2 中有效,但是在 rails 3 中我得到以下错误:

@order[commission_attributes]' is not allowed as an instance variable name</code></pre>

我似乎无法弄清楚问题是什么,或者如何解决这个问题。非常感谢任何帮助。

安德鲁

最佳答案

fields_for 方法只接受对象或符号。如果没有更多代码,很难给出具体的解决方案,但像这样的东西可能会起作用:

<%= form_for @order do |form| %>
...
<%= fields_for @order.commission do |commission_form| %>

在此处阅读:http://apidock.com/rails/v3.0.0/ActionView/Helpers/FormHelper/fields_for

关于ruby-on-rails - Rails 3 中的 fields_for 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9394377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com