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.
Related
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
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.
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
I am using simple_form on a project where I have the following associations
Company
has_many :users
has_many :projects
User
belongs_to :company
has_many :tasks
Project
belongs_to :company
has_many :tasks
Task
belongs_to :project
belongs_to :user
I am using a simple_form association input like so:
<%= f.association :user, :prompt => "Assign To...", :label_method => :first_name, :value_method => :id %>
But I need it to list ONLY the users who have the correct company_id (the same that the project belongs to). Is there a way to do this? I have done some googling and have come up with nothing so far.
I found an in-line way to handle it:
<%= f.association :user, :collection => User.where(:company_id => current_user.company_id), :prompt => "Assign To...", :label_method => :first_name, :value_method => :id %>
Only shows users who's company_id match that of the current user (which matches that of the project)
You should extract all the users , that belong to the company , and pass it to the list for assignment , is that right ? In your method (helper or model)
#company = Company.find(params[:id])
#labors = #company.users
It should return an array of instance objects , which you can use in the list .
I have a form for creating a Style. When creating the style the user must also choose some features for the style.
The features are pre-populated into a multi-select box inside the form. When the style is saved the through table should be updated with entries (with style_id and feature_id) for each feature selected on the form.
I have in my controller:
def new
#style = Style.new
#style.stylefeatures.build
end
def create
#style = Style.new(params[:style])
#style.stylefeatures.build
#style.save
end
...and in my style model
attr_accessible :stylefeatures_attributes
has_many :stylefeatures
has_many :features, :through => :stylefeatures, :foreign_key => :feature_id
accepts_nested_attributes_for :stylefeatures
... and in my stylefeature model
belongs_to :style
belongs_to :feature
accepts_nested_attributes_for :feature
... and in my feature model
attr_accessible :description, :fullname, :name
has_many :stylefeatures
has_many :styles, :through => :stylefeatures, :foreign_key => :style_id
... and in my create form
<%= m.simple_fields_for :stylefeatures do |p| %>
<%= p.input :feature_id, :label => "Features", :collection => Feature.all, :input_html => { :multiple => true } %>
<% end %>
Now when I save the new Style, the stylefeatures table is updated with the appropriate style_id, but with two useless entries. The first is an array with all of the feature ids which have been selected in the form. The second is a blank entry with the appropriate style_id and nothing in the feature_id column.
Do you have any clue of what I might be doing wrong, or how to spread the collected feature_id's into the table as desired?
in new action u r building only one stylefeature, so only one will be created with feature_id = '1,3,45,563' (ofc depends that features u selected)
second one is generated by #style.stylefeatures.build in create action
u can try to remove fields_for and simply use :feature_ids instead of :feature_id
<%= p.input :feature_ids, :label => "Features", :collection => #features, :input_html => { :multiple => true } %>
or
<%= input_tag "style[feature_ids][]" , :label => "Features", :collection => #features, :input_html => { :multiple => true } %>