Rails 3 - ActiveAdmin - displaying an ActiveRecord object instead of text

I currently have a Rails 3.0.10 application, and I have created two models with simple associations: has_many and belongs_to.

After installing the ActiveAdmin plugin and creating the appropriate Ruby resource files, I noticed some strange behavior.

Below you can see that the โ€œJob Fileโ€ belongs to โ€œfor Ernet Clientsโ€. All associations work correctly, but the display name appears as the actual ActiveRecord instead of the client name.

enter image description here

This is a view view when in the "Job File" section:

enter image description here

But if I move on to viewing the actual client, the correct text will appear:

enter image description here

There must be something wrong with the Job File resource that causes it, but I canโ€™t understand what it can be. After googling, I found this: http://groups.google.com/group/activeadmin/browse_thread/thread/2a261e070efa7bae

In JobFile.register, I specified the display name as follows:

filter :ernet_client, :display_name_methods => :display_name 

This did not work, and I went through all the other available names to no avail.

I was looking for ActiveAdmin and sass-rails dependencies, it seems the only one - but that is if you are using 3.1 and I am using Rails 3.0.10.

And finally, to make sure my associations really work, I opened the Rails console and created a job file:

 job = JobFile.new => #<JobFile hash returned> job.ernet_client_id = 2 => 2 job.ernet_client.client_name => Target 

Everything seems to work as it should.

Does anyone know how to solve this?

+6
source share
2 answers

Have you tried to define the to_s method in the ErnetClient model?

 def to_s display_name end 

It looks like this is a method that is called automatically, as if you typed to_s on one of these objects in the console, you would get a similar result.

+10
source

Here you can find an explanation Filter select show object instead of object name .

 # Active Admin makes educated guesses when displaying objects, this is the list of methods it tries calling in order setting :display_name_methods, [ :display_name, :full_name, :name, :username, :login, :title, :email, :to_s ] 

You can define display_name , full_name or ... (see list above) ... method in your model. ActiveAdmin is looking for one of them.

+3
source

Source: https://habr.com/ru/post/900354/


All Articles