Rails Activeadmin - custom association select box - ruby-on-rails

In my Rails application, I have the following model:
class Idea < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :ideas
end
I am creating ActiveAdmin CRUD for my Idea model with the custom form that looks something like that looks something like that:
form do |f|
f.inputs do
f.input :member
f.input :description
end
end
The requirement is to have the custom text for a content of the member association, i.e "#{last_name}, #{first_name}". Is it possible to customize my member select box to achieve it?
Any help will be appreciated.

Yes, that is possible. I assume you want to use a DropDown list box for members to select a user from User model.
form do |f|
f.inputs do
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}
f.input :description
end
end

For Active Admin You have to pass it as a collection of Hashes. Key in hash will be the text which you want to display and value will be the attribute id.
For Example:
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}.to_h
collection: [{name1: 1}, {name2: 2}, {name3: 3}]
Note: I have added to_h at end of the map which will convert a collection of arrays into a collection of hashes.

Related

Active Admin has_many selectable list of records

I have been trying for days now, I am new at ROR and active admin. So far I have been able to add and delete has_many relations for a new record. And I am using strong_parameters as well as accept_nested_attributes. I want the
Ability to add and delete relations for existing records as well.
Ideally there should be an autocomplete box that allows searching and selection of an existing meaning for this particular model.
My models are
Word
Meaning
WordMeaning
I only want the capability to attach meanings that are already available to a word?
class Word < ActiveRecord::Base
belongs_to :language
has_many :word_meanings
has_many :meanings ,through: :word_meanings
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs do
f.input :language
f.input :word
f.input :wordInScript
f.input :pronunciation, :required => false, :as => :file
end
f.inputs do
f.has_many :meanings, heading: 'Meanings', allow_destroy: true, new_record: true do |a|
a.input :meaning
a.input :language
end
end
f.actions
end
You can determine the collection of the select:
a.input :meaning, :as => :select, :collection => {#your collection in Hash, Array or ActiveRecord relation}.map{|key, value| [value, key] }
ActiveAdmin uses Formtastic:
https://github.com/justinfrench/formtastic#usage

updating two models from activeadmin

I have two models:
Model: Stores => Relavant fields: id, retailer_id
Model: Retailer => Relavant fields: name, id
The form I am trying to create is an activeadmin form which looks at the retailer name, then returns a list of stores that matches the name in check_boxes. What I want is the user the select the stores he wants associated with the retailer, and hit update, which will basically update the retailer_id in the stores model with the current retailer record.
If creating a new retailer, I want the checkboxes to show all stores where the retailer_id is blank.
In my retailer model, I have added
has_many :stores
accepts_nested_attributes_for :stores
in my stores model, I have added
belongs_to :retailer
here is what I currently have in my retailer activeadmin form
ActiveAdmin.register Retailer do
action_item do
link_to "View unassigned stores", "/admin/unassigned_stores"
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :name
f.input :description, :as => :text
f.input :photo, :label => "Retailer Logo", :as => :file, :hint => image_tag(retailer.photo.url)
f.input :retailer_id, :for => :Stores, :as => :check_boxes, :collection => Store.find(:all, :conditions => ["retailer_id is null and (name like ? or name_2 like ?) ", "%#{retailer.name}%", "%#{retailer.name}%"]), :label => "Assign Stores to retailer"
end
f.buttons
end
end
after a lot of hours searching around, came accross this little gem.
ActiveAdmin -- Show list of checkboxes for nested form instead of a form to add items
basically, i changed :retailer_id to :stores and added, :store_ids to attr:accessible and that allowed me to do what i was trying to do.

generate more than one select for HABTM - simple_form and RoR

When using simple_form and creating a field for a HABTM associated model, we obtain a select box that accepts multiple options.
Is there any way of having multiple select boxes that accept a single option?
If we have categories, for example:
Category1 => <select>options...</select>
Category2 => <select>options...</select>
Category3 => <select>options...</select>
etc...
Assuming the following models:
class Business < ActiveRecord::Base
has_and_belongs_to_many :categories
class Category < ActiveRecord::Base
has_and_belongts_to_many :businesses
You can use the following rails helper to show a select box for each category.
- #business.categories.each do |c|
= f.collection_select :category_ids, Category.all, :id, :name, {:selected => c.id}, {:name => 'business[category_ids][]'}
Then you can use javascript to dynamically create new select boxes. This railscast explains the basics.
Within a simple_form_for eg states/regions
class State < ActiveRecord::Base
has_and_belongs_to_many :regions
(leaving out unnecessary form elements for clarity)
<%= simple_form_for #state do |f| %>
<%= f.association :regions, as: :check_boxes, collection: Region.all.sort, :selected => #state.regions, :label => false %>
<% end %>
This will display all Regions (obviously you can filter it) as a list of checkboxes, with those already recorded being selected

Rails/Formtastic Newbie: Setting display field for referenced "select" box

New formtastic user here.
I have a relationship user has_many clients
In formtastic, if I do something such as
f.input :employer
it returns a select box of all employer object references. I'd like to display (last name, first name) instead. I'm sure this is very simple, but I can't figure out exactly how to do it.. Any help appreciated.
These didnt work for me, however, this did:
<%= f.input :user, :label => "Author", :label_method => :username %>
Also a little cleaner ^^
Or, you can set the display method once and for all on the model itself:
(In employer.rb)
def to_label
email
end
Try
f.input :employers, :as => :select, :collection => Employer.find(:all, :order => "last_name ASC")
or
f.input :employers, :as => :select, :collection => Employer.collect {|e| [e.last_name, e.id] }

Rails: How to use select in formtastic with activeRecord

I'm new to rails and I guess you can answer this question easily.
What I got so far is
= f.input :task, :as => :select, :collection => #tasks, :include_blank => true
where the tasks collection is defined by
Task.find(:all)
within in the controller.
This does in fact work, shows me a dropdown-list of all Tasks to select from and connects them.
The problem here is, that the dropdown menu shows me values like
#<Task:0x123456789d1234>
Where do I define what value is being displayed?
I believe you can use the :label_method to solve your problem...
f.input :task, :as => :select, :collection => #tasks,
:include_blank => true, :label_method => :title
where :title is what you want to show.
This may help a little more.
You can define a to_s method in the model:
class Task < ActiveRecord::Base
def to_s
title # the attribute to display for the label
end
end

Resources