Richlewis
Richlewis

Reputation: 15384

Active Admin and Associations

It's been a while since I have done anything with Rails and now I'm using rails 4. Also I'm taking a look at active admin. Looking for a little assistance to get me on my way.

I have two models:

class Membership < ActiveRecord::Base
  belongs_to :member
  attr_accessible :membership_type
end

class Member < ActiveRecord::Base
  has_many :memberships

  accepts_nested_attributes_for :memberships
  attr_accessible :forename, :middlename, :surname, :house_no, :house_name, :street,   :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email, :membership_attributes
end

The idea her is that when creating a new member you should be able to select a membership type form a drop down (there are 3 options)

My active admin resource look like this at the moment but I need to incorporate the new membership type:

ActiveAdmin.register Member do
  # Set Which Columns are to be displayed on the index page
  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
    default_actions
  end

  # Set Which Columns are to be displayed on Create New Member
  form do |f|
    f.inputs "Member Registration" do
    f.input :forename
    f.input :middlename
    f.input :surname
    f.input :house_no
    f.input :house_name
    f.input :street
    f.input :town
    f.input :postcode
    f.input :home_tel
    f.input :mobile_tel
    f.input :work_tel
    f.input :email
    f.input :memberships, :label => 'Membership Type', :as => :select, :collection => Membership.all
  end   
end

So my memberships select box appears in the form but is showing as:

#<Membership:123456> 

How do I get it to show the actual value?

Also slightly off the question topic but with rails 4 I no longer have to use attr_accessible do I, but for now I've included the gem 'protected_attributes'. Any pointers on this would be appreciated.

Upvotes: 1

Views: 4573

Answers (5)

Zain Awais
Zain Awais

Reputation: 40

You can do this by:

f.input :memberships, :label => 'Membership Type', :as => :select,:collection => proc {(Membership.all).map{|m| [t.membership.name,t.id]}}

Upvotes: 0

haley
haley

Reputation: 1593

To show the actual membership type's value use this,

f.input :memberships, :label => 'Membership Type', :member_label => :membership_type

Formtastic documentation has more examples.

Upvotes: 2

Josh Kovach
Josh Kovach

Reputation: 7749

If you are using Rails 4 and are just starting out with ActiveAdmin, I would recommend using the version from master branch. ActiveAdmin < 1.0.0-pre does not support Rails 4, and actually supports strong parameters via the permit_params dsl.

To use that version, add the following to your Gemfile

gem 'activeadmin', github: 'gregbell/active_admin'

In Rails 4, you won't be using attr_accessible anymore, and all your params will need to be permitted in the controller. Check the docs here for details:

https://github.com/gregbell/active_admin/blob/master/docs/2-resource-customization.md

ActiveAdmin.register Member do
  permit_params :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email, membership_ids
end

As for your membership not displaying properly, ActiveAdmin does at smart-lookup to try to determine the display name for something, trying for a method named name, title, display_name, to_label, to_s, and, I think,inspect` if all else fails. Simply add a method to your membership model:

def display_name
  membership_type
end

Upvotes: 3

Pravin Mishra
Pravin Mishra

Reputation: 8444

Hey Looks every thing fine, there is small typo in attr_accessible:

:membership_attributes -> :memberships_attributes

if possible put it at first place,

attr_accessible :memberships_attributes, :forename, :middlename, :surname, :house_no, :house_name, :street,   :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email

To allow the nesting and the updating of has_many associations you must add the following to the model that they belong to :

  1. Add an attr_accessible for the nested attribute to allow for mass assignment.

  2. Add the obvious has_many association with the option to destroy the related has_many associations if the belongs_to model is destroyed.

  3. Add the accepts_nested_attributes to allow a single request to update the has_many model/s and the belongs_to model with the option to destroy the has_many model if requested.

Upvotes: 1

user2996577
user2996577

Reputation: 61

To show membership in column view you can do something like this:

column :memberships do |member| 
  member.memberships.map { |ms| ms.type }.join(', ')
end

In Rails 4 instead of using the 'protected_attributes' gem you can put this code in the Active Admin initializer (active_admin.rb):

ActiveAdmin::ResourceController.class_eval do
# Allow ActiveAdmin admins to freely mass-assign when using strong_parameters
  [(params[resource_request_name] || params[resource_instance_name]).try(:permit!) || {}]
  end
end

Upvotes: 2

Related Questions