administrate: Errors for STI models (undefined method for paths)

I have models:

  • Topic < ActiveRecord::Base
  • Topics::News < Topic
  • Topics::Article < Topic

The $ rails generate administrate:install created following routes:

# config/routes.rb
# ...
  namespace :admin
    resources :topics
    root to: 'topics#index'
  end

And /admin/ had given me following error:

undefined method `admin_topics_article_path' for #<#<Class:0x007fb0bbf85e30>:0x007fb0c7d159f0>
Did you mean?  admin_topics_path

I think it’s problem with how Administrate or Rails handles single table inheritance and model_name of STI models.

My temporary workaround:

class Topic < ActiveRecord::Base
  def self.model_name
    return super if self == Topic
    Topic.model_name
  end
end

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 3
  • Comments: 21 (5 by maintainers)

Most upvoted comments

@dmitryzuev @henvo, i also had the same issue and i think where is the problem, @dmitryzuev in db/schema.rb in topics table, did you set default to “Article” ?

update: @carlosramireziii the probleme seems to occur when we set a default value to type field in the STI and i noticed 2 cases: first case: when we set the default in the migration (and db/schema.rb) Administrate will force us to use the model which inherit: for my case i used someting like that

User < ActiveRecord::Base
Administrator < User
Public < User

second case: because i set the type default to Public in the migration, Administrate will force me to use Public class in the dashboard or it will give us the undefined method ‘admin_public_path’ so, i resolved the problem by removing the default from the migration and put in the model class like this:

class User < ApplicationRecord
  attribute :type, :string, default: 'Public'
end

with this approach i can remove Public from routes ressources without having this error in the main page, but the error comes again when i try to create a new User.

i hope this will help you to understand to source of this error.

update: @carlosramireziii i fixed this issue with this solution in routes.rb:

Rails.application.routes.draw do
  namespace :admin do
    resources :users
    resources :users, as: :administrators
    resources :users, as: :publics

i hope this solution will not create security issues

@dmitryzuev in your case try to do this

namespace :admin
    resources :topics
    resources :topics, as: :articles
    resources :topics, as: :news

i hope it will also works for you

Update: the last solution i suggested generate another erros when i try to update a user with this error message param is missing or the value is empty: user

@dmitryzuev Following @mibradev suggestion I was able to find solution for my case. In your situation you should be able to do following:

# routes.rb
resources :topics
resources :topics_news, controller: :topics, path: :topics
resources :topics_articles, controller: :topics, path: :topics

This will give you the necessary routes.

The next problem is with edition form because it will send params with keys :topics_news or :topics_article but you need :topic. In this case you could add before_action to Administrate controller.

# topics_controller.rb
module Admin
  class TopicsController < Admin::ApplicationController
    before_action :set_aliases, only: [:edit, :update]

    def set_aliases
      params[:topic] = params[:topics_news] if params[:topics_news]
      params[:topic] = params[:topics_article] if params[:topics_article]
    end
end