ruby-on-railsrails-adminacts-as-taggable-on

rails_admin to recognize models with acts_as_taggable_on tag_lists


I'm setting up a rails application with rails_admin and acts_as_taggable_on gems.

Earlier in the project when attempting to make sure that this could be done I found this issue on the rails_admin github page which led to this gem which is still in the list of plugins on the rails_admin github wiki. So it seems like it can be done.

I started by following the setup instructions in the READMEs for rails_admin, acts_as_taggable_on and rails_admin_tag_list. I have some models on the app and before adding any tagging functionality to them they show up and work just fine on rails_admin.

Here is an example of a model that I'm attempting to add tags to:

class Location < ActiveRecord::Base
  acts_as_taggable
  acts_as_taggable_on :regions

  private

  def location_params
    params.require(:location).permit(:lat, :long, :tag_list => [], :region_list => [])
  end
end

In this format I get a flash notice: "Model 'Location' could not be found" when accessing the rails admin page. The Location Model also does not show up in the list of Models.

I have not yet built out pages to add data but I did add a location to my database via the rails console and added tags to the lists for that location using the example from the acts_as_taggable_on README:

a = Location.new
a.region_list.add("awesome")

After doing this and looking at the rails_admin page again I still see the "Model 'Location' could not be found" flash notice. But I also see Location in the list of models.

Clicking on Location gives me the following error:

NoMethodError at /location

undefined method `[]' for #<RailsAdmin::Adapters::ActiveRecord::Property:0x00000101fabd90>

Better errors shows the

if tag_types.include?(properties[:name]) 

line below to be the line at fault:

RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|

  model = parent.abstract_model.model

  if defined?(::ActsAsTaggableOn) && model.taggable?
    tag_types = model.tag_types

    if tag_types.include?(properties[:name])
      name = "#{properties[:name].to_s.singularize}_list".to_sym
      fields << RailsAdmin::Config::Fields::Types::TagList.new(parent, name, properties)

    end
  end

I'm still really quite new but it seems like there isn't a whole lot written about the interaction between these two gems at the moment. I'm grateful for any insight you can provide to get rails admin to be able to display models with tag_lists appropriately. Thanks!


Solution

  • Okay, let me preface this by saying I have no idea what I'm doing. But I might have a clue.

    rails_admin_tag_list hasn't been updated for Rails 4, which it looks like you're using (hello, strong parameters!). And I'm guessing you're using a pretty recent version of rails_admin, since you're on Rails 4.

    Based on the debugging work you've already done, it looks like the issue is with the RailsAdmin::Adapters::ActiveRecord::Property (or it's at least adjacent to your issue), which looks like it was added in newer versions of rails_admin. (Or at least that's what it looks like without thorough research.)

    There's an unmerged pull request on rails_admin_tag_list that looks like it addresses that issue here.

    So, what I'd suggest trying is specifying that fork/commit of rails_admin_tag_list in your Gemfile, like such:

    gem 'rails_admin_tag_list', :git => 'git://github.com/imouaddine/rails_admin_tag_list.git', :ref => 'a9a4e31af6fdd2124110d0dff81ab97950803e65'

    Good luck!