4

我試圖弄清楚如何使用https://github.com/sferik/rails_admin/wiki/Has-many-%3Athrough-association示例中給出的位置排序,但不使用受保護的屬性,而是使用Rails 4的強參數。如果我嘗試使用attr_accessible :block_ids頁面上給出的block_ids=函數,則會收到ActiveRecord::UnknownAttributeError錯誤消息unknown attribute :block_ids。很顯然,如果我使用attr_accessible :block_ids,它會要求我將protected_attributes添加到我的Gemfile中,這不是Rails 4的方式。在rails_admin中對rails中的has_many:through關係使用可定製4

有沒有人能夠使用強參數在Rails 4的rails_admin中進行可定製的位置工作?

回答

3

省略attr_accessible :block_ids並在底部應用替代解決方案適用於我。

PS:Rails的4.2.0與rails_admin 0.6.6

class Grid < ActiveRecord::Base 
    has_many :block_grid_associations, :dependent => :delete_all, :autosave => true, :include => :block 
    has_many :blocks, :through => :block_grid_associations 

    def block_ids=(ids) 
    unless (ids = ids.map(&:to_i).select { |i| i>0 }) == (current_ids = block_grid_associations.map(&:block_id)) 
     (current_ids - ids).each { |id| block_grid_associations.select{|b|b.block_id == id}.first.mark_for_destruction } 
     ids.each_with_index do |id, index| 
     if current_ids.include? (id) 
      block_grid_associations.select { |b| b.block_id == id }.first.position = (index+1) 
     else 
      block_grid_associations.build({:block_id => id, :position => (index+1)}) 
     end 
     end 
    end 
    end 

    rails_admin do 
    configure :block_grid_associations do 
     visible(false) 
    end 

    configure :blocks do 
     orderable(true) # only for multiselect widget currently. Will add the possibility to order blocks 
     # configuration here 
    end 
    end 
end 
相關問題