简体   繁体   中英

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

I'm trying figure out how to use ordering with position as given in the example at https://github.com/sferik/rails_admin/wiki/Has-many-%3Athrough-association , but without using protected attributes, and instead using Rails 4's strong parameters. If I try to use the block_ids= function given on the page without attr_accessible :block_ids , I get an ActiveRecord::UnknownAttributeError error with message unknown attribute :block_ids . Obviously if I use attr_accessible :block_ids , it asks me to add protected_attributes to my Gemfile, which wouldn't be the Rails 4 way.

Has anyone been able to make orderable position work in rails_admin for Rails 4 using strong parameters?

Omitting attr_accessible :block_ids and applying the alternative solution at the bottom works for me.

PS: Rails 4.2.0 with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM