Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top