简体   繁体   中英

Active Admin Show Page for one model is not working

I am integrating Active Admin into a Ruby on Rails app. I registered all my models and already set up index , filter and show for all the models. Everything is working, but for one model the admin/show page is not running.

When calling the show page from the admin/index page, I get:

NoMethodError in Admin/safts#show

Showing /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374@xxxxxx/gems/activeadmin-0.6.0/app/views/active_admin/resource/show.html.arb where line #1 raised:

undefined method `empty?' for #<Keyword:0x105498800>
Extracted source (around line #1):

1: insert_tag renderer_for(:show)

Request

Parameters:

{"id"=>"9"}

The relative entry in my log file is:

Started GET "/admin/safts/9" for 127.0.0.1 at Sun Feb 25 14:48:04 +0100 2018
Processing by Admin::SaftsController#show as HTML
  Parameters: {"id"=>"9"}
  [1m[35mAdminUser Load (0.6ms)[0m  SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 2 LIMIT 1
  [1m[36mSaft Load (0.2ms)[0m  [1mSELECT `safts`.* FROM `safts` WHERE `safts`.`id` = ? LIMIT 1[0m  [["id", "9"]]
  [1m[35mKeyword Load (0.3ms)[0m  SELECT `keywords`.* FROM `keywords` WHERE `keywords`.`id` = 138 LIMIT 1
  Rendered /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374@xxxxxx/gems/activeadmin-0.6.0/app/views/active_admin/resource/show.html.arb (3.1ms)
Completed 500 Internal Server Error in 10ms

ActionView::Template::Error (undefined method `empty?' for #<Keyword:0x1052a0890>):
1: insert_tag renderer_for(:show)
  activemodel (3.2.5) lib/active_model/attribute_methods.rb:407:in `method_missing'
  activerecord (3.2.5) lib/active_record/attribute_methods.rb:149:in `method_missing'
  activeadmin (0.6.0) lib/active_admin/views/pages/show.rb:38:in `default_title'
  activeadmin (0.6.0) lib/active_admin/views/pages/show.rb:14:in `title'
  activeadmin (0.6.0) lib/active_admin/views/pages/base.rb:25:in `build_active_admin_head'
  arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:49:in `within'
  activeadmin (0.6.0) lib/active_admin/views/pages/base.rb:24:in `build_active_admin_head'
  activeadmin (0.6.0) lib/active_admin/views/pages/base.rb:9:in `build'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:30:in `build_tag'
  arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:26:in `build_tag'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:39:in `insert_tag'
  activeadmin (0.6.0) app/views/active_admin/resource/show.html.arb:1:in `___sers__tephan__rvm_gems_ruby_______p____saftzine_gems_activeadmin_______app_views_active_admin_resource_show_html_arb___1026777847_2195984600'
  arbre (1.0.1) lib/arbre/context.rb:45:in `instance_eval'
  arbre (1.0.1) lib/arbre/context.rb:45:in `initialize'
  activeadmin (0.6.0) app/views/active_admin/resource/show.html.arb:1:in `new'
  activeadmin (0.6.0) app/views/active_admin/resource/show.html.arb:1:in `___sers__tephan__rvm_gems_ruby_______p____saftzine_gems_activeadmin_______app_views_active_admin_resource_show_html_arb___1026777847_2195984600'
  actionpack (3.2.5) lib/action_view/template.rb:145:in `send'
  actionpack (3.2.5) lib/action_view/template.rb:145:in `render'
  activesupport (3.2.5) lib/active_support/notifications.rb:125:in `instrument'
.
.
.
  script/rails:6:in `gem_original_require'
  script/rails:6:in `require'
  script/rails:6


  Rendered /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374@xxxxxx/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.7ms)
  Rendered /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374@xxxxxx/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.6ms)
  Rendered /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374@xxxxxx/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.8ms)

The rails model is:

class Saft < ActiveRecord::Base
    attr_accessible :colour, :cover_alt, :description, :number, :short, :title_id

# Associations
    has_and_belongs_to_many :keywords, :join_table => "safts_keywords" 
    has_many :authors, :through => :texts 
    has_many :texts 
    belongs_to :title, :class_name => "Keyword", :foreign_key => "title_id"
    has_one :cover
    has_many :stamps
    has_many :images
end

The ActiveAdmin resource is:

ActiveAdmin.register Saft do
    index do
        column "Issue", :number 
        column "Title", :title_id do |saft|
            link_to saft.title.word.capitalize, saft_path(saft)
        end
        column :short
        column :description
        column :colour
        column :cover_alt
        default_actions
    end

    # Filter only by:
    filter :title_id, :label => 'Title', :as => :select, :collection => Saft.all.map{|u| ["#{u.title.word.capitalize}", u.id]}
    filter :short

    form do |f|
        f.inputs "Saft Details" do
            f.input :number, :label => "Number of issue"
            f.input :title_id, :label => 'Title', :as => :select, :collection => Keyword.all.map{|u| ["#{u.word.capitalize}", u.id]}
            f.input :short
            f.input :description
            f.input :colour, :label => "Colour (in hex)"
            f.input :cover_alt
        end
        f.actions
    end

    show do
        panel "Saft Details" do
            attributes_table_for saft do
                row :id
                row :number
                row :title_id
                row :short
                row :description
                row :colour
                row :cover_alt
                row :created_at
                row :updated_at
            end
        end
        active_admin_comments
    end
end

Just for context: SAFT is a magazine, with texts, images, authors, etc. All the other resources are working well in Admin. Only the show page of SAFT is not working. What could it be?

I was able to solve the problem, which is related to the associations.

The DisplayHelper Module of the gem has to be modified as follows:

module ActiveAdmin
  module ViewHelpers
    module DisplayHelper

      # Attempts to call any known display name methods on the resource.
      # See the setting in `application.rb` for the list of methods and their priority.
      def display_name(resource)
        resource.send display_name_method_for resource
      end

      # Looks up and caches the first available display name method.
      def display_name_method_for(resource)
        @@display_name_methods_cache ||= {}
        @@display_name_methods_cache[resource.class] ||= begin
          methods = active_admin_application.display_name_methods - association_methods_for(resource)
          methods.detect{ |method| resource.respond_to? method }
        end
      end

      # To prevent conflicts, we exclude any methods that happen to be associations.
      def association_methods_for(resource)
        return [] unless resource.class.respond_to? :reflect_on_all_associations
        resource.class.reflect_on_all_associations.map(&:name)
      end

      # Return a pretty string for any object
      # Date Time are formatted via #localize with :format => :long
      # ActiveRecord objects are formatted via #auto_link
      # We attempt to #display_name of any other objects
      def pretty_format(object)
        case object
        when String
          object
        when Arbre::Element
          object
        when Date, Time
          localize(object, :format => :long)
        when ActiveRecord::Base
          auto_link(object)
        else
          display_name(object)
        end
      end
    end
  end
end

This is because in the SAFT model there is an association with the name title , which goes into conflict with the display_name_method.

Your log entry says Keyword does not respond to empty? in Pages::Show#default_title . default_title calls display_name , have you overridden display_name_methods ? In this case you can remove :title

In config/initializers/active_admin :

config.display_name_methods = [ :display_name,
                                  :full_name,
                                  :name,
                                  :username,
                                  :login,
                                  :email,
                                  :to_s ]

Perhaps better in this case you can also try setting the title explicitly, eg.

  show title: :short do ...

I notice you are on very old versions of Rails and ActiveAdmin, I hope you will consider upgrading .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM