2014-09-20 50 views
0

我目前正在开发基于轨道的应用程序来管理订单。 (Rails的4.1.5和ActiveAdmin)来自不同型号的Rails 4新记录

我有这些模型:

class Customer < ActiveRecord::Base 
    has_many :estimates 
    has_many :orders 
    accepts_nested_attributes_for :estimates, :allow_destroy => true 
    accepts_nested_attributes_for :orders, :allow_destroy => true 
end 
class Order < ActiveRecord::Base 
    belongs_to :customer 
    has_many :line_items, as: :cartable 
    accepts_nested_attributes_for :line_items, :allow_destroy => true 
end 
class Estimate < ActiveRecord::Base 
    belongs_to :customer 
    has_many :line_items, as: :cartable 
    accepts_nested_attributes_for :line_items, :allow_destroy => true 
end 

我想要做的是创建基于估算纪录新秩序。该事情的工作,如果我创建一个新的订单,并显示在编辑页面:

member_action :confirm, :method => :get do 
    old_estimate = Estimate.find(params[:id]) 
    new_estimate = Order.create(old_estimate.attributes.merge({:id => nil, :created_at => 
    nil,:updated_at => nil})) 
    old_estimate.line_items.each do |li| 
    new_estimate.line_items.create(li.attributes.merge({:id => nil, :created_at => nil, 
     :updated_at => nil})) 
    end 
    redirect_to edit_customer_order_path(new_estimate.customer, new_estimate) 
end 

,但我想用“新”行动,创建后,才进行了编辑,并确认记录。

我试图用

redirect_to new_customer_order_path(old_estimate.customer, old_estimate.attributes) 

,它会呈现新的形式,但没有任何参数。 PARAMS在URL中,但我在日志中收到了“未经许可的参数:”。所有参数允许在Active Admin下(或者在other.rb和estimate.rb下):

permit_params :id, :customer_id, :title, :edd, :total, 
      line_items_attributes: [:id, :cartable_id, :cartable_type, :product_type, :source_lang, :dest_lang, :unit_price, :total, :_destroy] 

任何人有任何建议吗?

+0

你能解决这个@paulwang吗?我面临着类似的困境。 – neo 2016-03-10 17:41:22

回答

0

您可以覆盖新的动作设置形式的一些默认值:

controller do 
    def new 
    record = YourActiveRecordModel.new #or find, if you pass id 
    record.some_attribute = some_value 
    new! 
    end 
end 

相应的输入将被填补。