how add tag model (ActsAsTaggableOn) in rails_admi

2019-08-06 04:54发布

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 ?!

3条回答
看我几分像从前
2楼-- · 2019-08-06 05:13

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楼-- · 2019-08-06 05:13

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
查看更多
做个烂人
4楼-- · 2019-08-06 05:18

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
查看更多
登录 后发表回答