how add tag model (ActsAsTaggableOn) in rails_admin?

倖福魔咒の 提交于 2019-12-11 07:53:50

问题


I install rails admin and include all model in active record in rails admin,

first time, when install rails admin display me this error

NoMethodError in RailsAdmin::MainController#index

undefined method `per_page_kaminari' for #<ActiveRecord::Relation:0x00000006713e18>

and fixed it by added this configure

Kaminari.configure do |config|
    config.page_method_name = :per_page_kaminari
end

when install acts_as_taggable_on gem in project, rails admin did't add tag model in it ( i added field tags_list in other model), but i want to add tag model in rails admin to manage tags (index,create,edit and destroy)..

I added this line

config.included_models = ['ActsAsTaggableOn::Tag'] 

in rails_admin.rb and it display me tag model in dashboard, but when open list tags, it display me again this error

NoMethodError in RailsAdmin::MainController#index

undefined method `per_page_kaminari' for #<ActiveRecord::Relation:0x00000006713e18>

what should i do to add tag model in rails admin and manage tags ?!


回答1:


I had to solve the same issue here. I used the gem 'rails_admin_tag_list' but latest version from rubygem is not updated for rails 4 (doesnt support RailsAdmin Property). So you need to get the one from master that include the fix.

gem 'rails_admin_tag_list', git: 'https://github.com/kryzhovnik/rails_admin_tag_list.git', branch: 'master'

Indeed add:

config.included_models = ['YourModel', 'ActsAsTaggableOn::Tag']

and

class YourModel < ActiveRecord::Base
  acts_as_taggable # dont add attr_accessible stuff since we're in rails 4
end

then choose the config for tag list:

config.model 'YourModel' do
  configure :tag_list  do
    partial 'tag_list_with_autocomplete'
  end
  exclude_fields :body, :locale, :base_tags, :tags
end



回答2:


The exact same issue occurs with ActiveAdmin: Manage acts_as_taggable tags with activeadmin. I solved it with the same solution:

Add an initializer to config/initializers with the custom Kaminari code:

Kaminari.configure do |config|
    config.page_method_name = :per_page_kaminari
end

Create a new class in app/models called tag.rb:

# This class fixes a bug between Kaminari, RailsAdmin, and ActsAsTaggableOn.
class Tag < ActiveRecord::Base
end

Use the Tag model instead of ActsAsTaggableOn::Tag in RailsAdmin config:

config.included_models = [Tag] 



回答3:


Here's my solution with rails 4 and fields configuration (must be before the congig.included_models call):

config.model ActsAsTaggableOn::Tag do 
    edit do 
      exclude_fields :taggings_count
      exclude_fields :taggings
    end
  end

  config.model ActsAsTaggableOn::Tagging do 
    edit do 
      exclude_fields :context
    end
  end

  admin_models = ActiveRecord::Base.descendants.map(&:name)
  admin_models.delete("PgSearch::Document") #if you use it
  admin_models.delete("PaperTrail::Version") #if you use it
  #add other models to exclude here
  config.included_models = admin_models


来源:https://stackoverflow.com/questions/19077900/how-add-tag-model-actsastaggableon-in-rails-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!