Show value in Active Admin

1.1k views Asked by At

I am trying to show the value of an attribute via its id with formtastic. I have two models hat are setup like this

class Membership < ActiveRecord::Base
has_many :members
attr_accessible :membership_type

end

class Member < ActiveRecord::Base
belongs_to :membership
accepts_nested_attributes_for :membership
attr_accessible :membership_id, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email
end

My index view is set up like this so far

index do
column :forename
column :middlename
column :surname
column :house_no
column :house_name
column :street
column :town
column :postcode
column :home_tel
column :mobile_tel
column :work_tel
column :email
column :membership 

default_actions

end

but the value for the membership is output as

#<Membership:0x007f2c2064c370>

How would i get the actual value so it will say "Student" for example

Normally i would do something like this i guess

<% @members.each do |m| %>
<%= m.forname%>
<%= m.surname%>

<% m.memberships.each do |s| %>
<%= s.membership_type%>


<% end %>

but cant figure it out within formtastic

Thanks

3

There are 3 answers

0
Michał Albrycht On BEST ANSWER

Try this:

column 'Membership type' do |member|
    "#{member.membership.membership_type}".html_safe
end
column 'Other members' do |member|
    member.membership.members.each() do |m|
      "#{m.forname} #{m.surname}<br/>".html_safe
    end
end
0
Charles Maresh On

ActiveAdmin tries its best to find a display name for your objects using a list of possible attributes:

  • :display_name
  • :full_name
  • :name
  • :username
  • :login
  • :title
  • :email

To ensure your Membership object has a recognizable name throughout the system you can add one of those attributes as a method to Membership:

class Membership < ActiveRecord::Base
  has_many :members
  attr_accessible :membership_type

  def display_name
    membership_type
  end
end

Alternatively, you can do like Michał Albrycht suggests and configure views to perform custom rendering for the membership column.

0
Mashpy Rahman On

Thanks @Cmaresh. Problem is solved. This is the commit link where i solved the problem in my project. Any one can see the source code and can easily understand how to solve the problem.