簡體   English   中英

在rails_admin中使用orderable獲取rails 4中的has_many:through關系

[英]Using orderable in rails_admin for a has_many :through relationship in rails 4

我正在嘗試弄清楚如何使用位置,如https://github.com/sferik/rails_admin/wiki/Has-many-%3A through-association中的示例所示,但不使用受保護的屬性,而是使用Rails 4的強大參數。 如果我嘗試使用沒有attr_accessible :block_ids的頁面上給出的block_ids=函數,我會收到一條帶有unknown attribute :block_idsActiveRecord::UnknownAttributeError錯誤unknown attribute :block_ids 顯然,如果我使用attr_accessible :block_ids ,它會要求我將protected_attributes添加到我的Gemfile中,這不是Rails 4方式。

有沒有人能夠使用強參數在rails_admin中為Rails 4制作可訂購位置?

省略attr_accessible :block_ids並在底部應用替代解決方案對我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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM