ruby-on-rails – 在rails_admin中使用orderable获取has_many:
发布时间:2020-12-17 02:21:49 所属栏目:百科 来源:网络整理
导读:我正在尝试弄清楚如何在 https://github.com/sferik/rails_admin/wiki/Has-many-%3Athrough-association的示例中给出如何使用位置排序,但不使用受保护的属性,而是使用Rails 4的强参数.如果我尝试使用没有attr_accessible:block_ids的页面上给出的block_ids
我正在尝试弄清楚如何在
https://github.com/sferik/rails_admin/wiki/Has-many-%3Athrough-association的示例中给出如何使用位置排序,但不使用受保护的属性,而是使用Rails 4的强参数.如果我尝试使用没有attr_accessible:block_ids的页面上给出的block_ids =函数,我会收到一条带有未知属性的ActiveRecord :: UnknownAttributeError错误:block_ids.显然,如果我使用attr_accessible:block_ids,它会要求我将protected_attributes添加到我的Gemfile中,这不是Rails 4方式.
有没有人能够使用强参数在rails_admin中为Rails 4制作可订购位置? 解决方法
省略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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |