2016-08-04 81 views
0

这是我的模型:如何设置permit_params另一个模型

Product.rb

class Product < ApplicationRecord 
    belongs_to :position 
    has_many :products_sizes 
    has_many :sizes, through: :products_sizes 
    has_many :reviews, dependent: :destroy 
    accepts_nested_attributes_for :products_sizes 
end 

Products_size.rb

class ProductsSize < ApplicationRecord 
    belongs_to :product 
    belongs_to :size 
    has_many :prices 
    accepts_nested_attributes_for :prices 
end 

Size.rb

class Size < ApplicationRecord 
    has_many :products_sizes 
    has_many :products, through: :products_sizes 
end 

Price.rb

class Price < ApplicationRecord 
    belongs_to :products_size 
end 

在ActiveAdmin我需要为产品的形式,当我更新的产品,我可以创造一个价格,所以表格外观的一部分像这样:

... #here is the begining of the form 
f.inputs 'Sizes' do 
      f.semantic_fields_for ProductsSize.where(product_id: params[:id], size_id: Product.find(params[:id]).products_sizes.size.to_i).first.prices.new do |ps| 
      ps.input :products_size_id, label: 'Size', as: :select, collection: Product.find(params[:id]).sizes.map { |s| ["#{s.title}", s.id] } 
      ps.input :quantity 
      ps.input :amount 
      li do 
       link_to 'Add size', '#' 
      end 
      end 
     end 

这一切似乎都很好,除非单击提交按钮时,价格未创建。我想,那是因为permit_params未指定为price。我如何指定它们?谢谢。

回答

0

这里是你如何让在active admin

ActiveAdmin.register Post do 
permit_params :title, :content, :author 
end 

参数这只是一个例子,使用自己的PARAMS

+0

是的,但我需要设置'permit_params'不是为我的'Product'模型,而是'Price',它具有'belongs_to:products_size'关系。我通过'ProductsSize'模型达到'价格'。 –

+0

你允许控制器中的参数吗? –

0
ActiveAdmin.register Post do 
    permit_params :title, 
       comments_attributes: [:name, :hidden] 
end 

邮政是一个moodel和评论是另一回事。如果您的模型名称是价格,您可以使用price_attributes:[... params ...],那么您可以在comments_attributes的注释中使用参数。

相关问题