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____somesite_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____somesite_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?
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.
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.
I am in a process of migrating my application from ruby 1.8.7 to 1.9.3 (and later upgrading Rails). However I geht some problems with logging in 1.9.3.
By using the following code in environment.rb when console or server starts up
logfile = File.open(File.join(RAILS_ROOT,'/log/call_log.log'), 'a')
CALL_LOGGER = AuditLogger.new(logfile)
CALL_LOGGER.info "CALL_LOGGER: Server started."
I get
/usr/local/rvm/scripts/irbrc.rb:32:in `write': not opened for writing (IOError)
AuditLogger is defined as follows:
class AuditLogger < Logger
def format_message(severity, timestamp, progname, msg)
"#{timestamp.strftime("%Y-%m-%d %H:%M:%S")} #{progname} #{severity} #{msg}\n"
end
end
If I do this manually with irb, this works
1.9.3-p551 :006 > logfile = File.open(File.join('/var/my_app/log/call_log.log'), 'a')
=> #<File:/var/my_app/log/call_log.log>
=> #<AuditLogger:0x00000001ad95e8 #progname=nil, #level=0, #default_formatter=#<Logger::Formatter:0x00000001ad95c0 #datetime_format=nil>, #formatter=nil, #logdev=#<Logger::LogDevice:0x00000001ad9570 #shift_size=nil, #shift_age=nil, #filename=nil, #dev=#<File:/var/my_app/log/call_log.log>, #mutex=#<Logger::LogDevice::LogDeviceMutex:0x00000001ad9548 #mon_owner=nil, #mon_count=0, #mon_mutex=#<Mutex:0x00000001ad94f8>>>>
1.9.3-p551 :011 > CALL_LOGGER.info "CALL_LOGGER: Server started."
=> true
I suspect that the class may have been overriden by another class? So I added some code to determine the class of the file which was currently used
#file.class.ancestors
[File, IO, File::Constants, Enumerable, Object, ActiveSupport::Dependencies::Loadable, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]
#file.methods.sort
results in
[:!, :!=, :!~, :<<, :<=>, :==, :===, :=~, :__id__, :__send__, :`, :acts_like?, :advise, :all?, :any?, :as_json, :atime, :autoclose=, :autoclose?, :binmode, :binmode?, :blank?, :breakpoint, :bytes, :chars, :chmod, :chown, :chunk, :class, :class_eval, :clone, :close, :close_on_exec=, :close_on_exec?, :close_read, :close_write, :closed?, :codepoints, :collect, :collect_concat, :copy_instance_variables_from, :count, :ctime, :cycle, :daemonize, :dclone, :debugger, :define_singleton_method, :detect, :display, :drop, :drop_while, :dup, :duplicable?, :each, :each_byte, :each_char, :each_codepoint, :each_cons, :each_entry, :each_line, :each_slice, :each_with_index, :each_with_object, :enable_warnings, :entries, :enum_for, :eof, :eof?, :eql?, :equal?, :exclude?, :extend, :extend_with_included_modules_from, :extended_by, :external_encoding, :fcntl, :fdatasync, :fileno, :find, :find_all, :find_index, :first, :flat_map, :flock, :flush, :freeze, :frozen?, :fsync, :getbyte, :getc, :gets, :grep, :group_by, :hash, :html_safe?, :include?, :index_by, :initialize_clone, :initialize_dup, :inject, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_values, :instance_variable_defined?, :instance_variable_get, :instance_variable_names, :instance_variable_set, :instance_variables, :internal_encoding, :ioctl, :is_a?, :isatty, :kind_of?, :lineno, :lineno=, :lines, :load_with_new_constant_marking, :lstat, :many?, :map, :max, :max_by, :member?, :metaclass, :metaclass_with_deprecation, :metaclass_without_deprecation, :method, :methods, :min, :min_by, :minmax, :minmax_by, :mtime, :nil?, :none?, :object_id, :one?, :partition, :path, :pid, :pos, :pos=, :presence, :present?, :pretty_inspect, :pretty_print, :pretty_print_cycle, :pretty_print_inspect, :pretty_print_instance_variables, :print, :printf, :private_methods, :protected_methods, :psych_to_yaml, :psych_y, :public_method, :public_methods, :public_send, :putc, :puts, :read, :read_nonblock, :readbyte, :readchar, :readline, :readlines, :readpartial, :reduce, :reject, :remove_subclasses_of, :reopen, :require, :require_association, :require_dependency, :require_library_or_gem, :require_or_load, :respond_to?, :respond_to_missing?, :returning, :reverse_each, :rewind, :seek, :select, :send, :set_encoding, :silence_stderr, :silence_stream, :silence_warnings, :singleton_class, :singleton_methods, :size, :slice_before, :sort, :sort_by, :stat, :subclasses_of, :sum, :suppress, :syck_to_yaml, :sync, :sync=, :sysread, :sysseek, :systemu, :syswrite, :taguri, :taguri=, :taint, :tainted?, :take, :take_while, :tap, :tell, :to_a, :to_enum, :to_hash, :to_i, :to_io, :to_json, :to_param, :to_path, :to_query, :to_s, :to_set, :to_yaml, :to_yaml_properties, :to_yaml_style, :truncate, :trust, :try, :tty?, :ungetbyte, :ungetc, :unloadable, :untaint, :untrust, :untrusted?, :with_options, :write, :write_nonblock, :zip]
According to Ruby 1.9.3 File class definition there should be some methods like 'readable?', 'writable?' in the method's list. But they aren't.
Here is also my list of rubygems which may be helpful
actionmailer (2.3.18)
actionpack (2.3.18)
activerecord (2.3.18)
activeresource (2.3.18)
activesupport (2.3.18)
archive-tar-minitar (0.5.2)
bigdecimal (1.1.0)
bundler (1.7.6)
bundler-unload (1.0.2)
cgi_multipart_eof_fix (2.5.0)
charlock_holmes (0.6.9)
columnize (0.9.0)
daemons (1.0.10)
debugger-ruby_core_source (1.3.8)
eventmachine (1.0.3)
executable-hooks (1.3.2)
fastercsv (1.5.5)
fastthread (1.0.7)
gem-wrappers (1.2.7)
gem_plugin (0.2.3)
geoip-c (0.9.1)
hoe (2.7.0)
hpricot (0.8.6)
httpclient (2.3.4.1)
image_size (1.2.0)
imagesize (0.1.1)
io-console (0.3)
io-tail (0.0.3)
json (1.8.1, 1.5.5)
json_pure (1.8.2, 1.8.1)
juggernaut (2.1.1)
linecache19 (0.5.12)
macaddr (1.6.7)
mechanize (2.0.1)
memcache-client (1.8.5)
mime-types (1.16)
mini_portile (0.6.2, 0.5.2)
minitest (2.5.1)
mongrel (1.2.0.pre2)
mysql (2.9.1)
net-http-digest_auth (1.4)
net-http-persistent (1.9)
nokogiri (1.6.6.2, 1.5.0)
oj (2.5.5)
rack (1.1.6)
rails (2.3.18)
rake (10.1.1, 0.9.2.2)
rbx-require-relative (0.0.9)
rcov (0.9.11)
rdoc (4.1.1, 3.9.5)
redis (3.2.1, 3.0.7)
ruby-debug-base19 (0.11.25)
ruby-debug-base19x (0.11.31)
ruby-debug-ide (0.4.22)
ruby-ole (1.2.11.8)
ruby_core_source (0.1.5)
rubyforge (2.0.4)
rubygems-bundler (1.4.4)
rubyzip (1.1.7)
rvm (1.11.3.9)
soap4r (1.5.8)
spreadsheet (0.9.7)
svg-graph (1.0.5)
systemu (2.6.5, 2.6.4)
thin (1.6.2)
tins (0.13.2)
uuid (2.3.7)
vpim (13.11.11)
webrobots (0.1.1)
xmpp4r (0.5)
Question: Anybody knows how to overcome this issue?
You're using an old version of rails, I'm not sure how things were handled back then, but Logger.new should accept a simple filename as a string.
Forget File.open and let it figure out how to handle the file:
logfile = File.join(Rails.root, '/log/call_log.log')
CALL_LOGGER = AuditLogger.new(logfile)
CALL_LOGGER.info "CALL_LOGGER: Server started."