2016-08-04 45 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的註釋中使用參數。

相關問題