I am using active admin gem.
i have questions table that have relationship with user_id, when i want to add new questions the select element is showing with tons of users, there is a way to make this select element along with search input for user name?
ActiveAdmin.register Question do
permit_params :difficulty, :title, :user
form do |f|
actions
inputs 'Question Details' do
input :user, :as => :select, :collection => User.all
input :difficulty, as: :select, collection: [1,2,3,4]
input :title
end
actions
end
end
You can use a javascript plugin, I'd thoroughly recommend chosen
It will dynamically turn a regular form select into a searchable select with a simple Javascript call.
You can checkout some demos here:
http://harvesthq.github.io/chosen/
You can integrate it into activeadmin by adding the chosen-rails gem to your Gemfile
gem 'chosen-rails'
Then add the javascript to your app/assets/javascripts/active_admin.js.coffee
#= require chosen-jquery
Add the css to your app/assets/stylesheets/active_admin.scss
#import "chosen"
You can add it to all your select inputs by simply adding the following to app/assets/javascripts/active_admin.js.coffee
$ ->
$("#active_admin_content select").chosen()
You could also limit it to certain select inputs by adding a class to active admin inputs and only targeting those selects with a specific class like chosen-select
Related
I connected globalize to my project. The main aim was to translate some fields in my Book.rb Model via ActiveAdmin. I also added select input to Book page in Active Admin with set of locales (I18n). How can I pass locale when I submit form (updates Book), so that translation would be saved in that locale?
Globalize documentation doesn't have enough examples. They suggest we can use Book.attributes{title: "some title", locale: :en}. But I have no idea how to implement that for my case.
admin/Book.rb
...
permit_params :title, :pages
form do |f|
inputs "Book info" do
input :title
input :pages
input :set_locale as :select, collection => [:ru, :en]
end
actions
end
...
So I have a Company, Subcompany models, and I use the Brand model as the masterlist for all company and subcompanies. Right now, when the admin user creates a new company, they have to use the dropdown list of brands to put a new company or subcompany into the list.
This becomes unscalable as there are 10000's of companies. The easiest way to filter out companies is to filter by category. so assuming all brands have a category attribute, I was wondering if there is any way, within the ActiveAdmin framework, to allow the admin user to filter the dropdown list by a category?
Here is what I have so far, it allows me to do a drop down on all the brands. But I want a way for the user to dynamically shrink the list by picking a category.
form do |f|
f.inputs do
f.input :name, :as => :select, :collection => Brand.all.collect {|brand| brand.name }
f.has_many :sub_companies, allow_destroy: true do |sub|
sub.input :name, :as => :select, :collection => Brand.all.collect {|brand| brand.name}
end
end
actions
end
There is no build in way, you can do one of the following thinks:
User select2, a suggest/search supporting select field, you can find some integration help here
You can write a javascript to fill a second select by the value of the first one.
I would like to create a dropdown menu for a list of countries in my signup page with devise. I understand that I need to create a migration
rails g migration add_countries_to_user country:string
and then I have to use create the form in my view page
<%= f.select :countries, options_for_select(%w[Alfganistan, Albania, Algeria...]) %>
I would like to know if my form correct and where can I put the countries list in because it is not right to write 200+ countries in the view page right?
Thanks.
As suggested, you can use country_select. Or, you can do it on your own as:
Create an initializer which contains list of countries (or anything in particular you want) config/initializers/countries.yml
countries:
- Afghanistan
- United States
- ...
Load it in database by creating a rake task as:
lib/tasks/load_countries.rb
namespace :db do
desc "Loads countries in database"
task :load_countries => :environment do |t|
countries_list = YAML.load("#{Rails.root}/config/initializers/countries.yml")['countries']
countries.each do |country|
Country.find_or_create_by_name(country)
end
end
end
Whenever you add any countries in yml, you can populate it by invoking this rake task: rake db:load_countries.
Maintain a model Country :
class Country < ActiveRecord::Base
validates :name, presence: true, uniqueness: { case_insensitive: true }
end
I am considering that a user belongs_to 1 country above, and a country has_many users. In your view, :
f.select :country, options_from_collection_for_select(Country.all, :id, :name)
Note: I am using association approach above, since it will make it easier to make queries against this field in future, unlike saving an actual string in user.
Use the country_select gem.
# Gemfile
gem 'country_select'
form:
country_select("user", "country")
Apart from gem and Countries read from YML.
One more option is creating a method in your helper
File : app/helpers/country_helper.rb
def get_countries
{:1=>Africa,:2=>"America"}
end
In Views you can use this way
<%= options_from_collection_for_select(get_countries, :id, :name) %>
Look up rails cast #88 revised dynamic select menus. What you need is method call grouped_collection_select in which you will map out the item you need based on how they corresponded to one another
You could do this as a helper method. eg, in your users_helper.rb you could list the selections:
def country_options
[
['Afghanistan'],
['Albania'],
...
['Zimbabwe']
]
end
Then, your selector pulls from that helper method:
<%= f.select :country, options_for_select(country_options), { prompt: 'Choose Country' } %>
So I have a CareerEntry model that has a fullintern attribute, which is a string that is supposed to specify whether the entry represents an internship or a full-time position. I limit the values that can appear in this attribute as follows:
validates_inclusion_of :fullintern, :in => ["Internship", "Full-time"]
However, in ActiveAdmin, the part in the edit form that deals with the fullintern attribute still has a text field. How do I make it a dropdown box where the admin can select either "Internship" or "Full-time"?
Thanks.
You can use Formtastic's input helpers to use a select input:
form do |f|
f.inputs "Details" do
f.input :fullintern, as: :select, collection: ["Internship", "Full-time"]
end
f.actions
end
See Formtastic's usage section for the full set of native capabilities.
I have a Post model which I'm accessing through ActiveAdmin. It's also taggable using the acts_as_taggable_on gem. So the admin can add, edit or delete tags from a specific Post.
The normal way to add the tagging functionality for the resource in your admin panel is by doing this in admin/posts.rb:
ActiveAdmin.register Post do
form do |f|
f.inputs "Details", :multipart => true do
f.input :tag_list
# and the other irrelevant fields goes here
end
f.buttons
end
end
However, I want to have the tags selected from a multiple select form field and not being entered manually in a text field (like it is with the code above). So I've tried doing this:
f.input :tag_list, :as => :select,
:multiple => :true,
:collection => ActsAsTaggableOn::Tag.all
but it doesn't work as expected. This actually creates new tags with some integer values for names and assigns them to that Post. Someone told me that extra code is needed for this to work.
Any clues on how this is done? Here's my model just in case: http://pastie.org/3911123
Thanks in advance.
Instead of
:collection => ActsAsTaggableOn::Tag.all
try
:collection => ActsAsTaggableOn::Tag.pluck(:name)
Setting the collection to Tag.all is going to tag your posts with the tag's ID, since that's how tags are identified by default (that's where the integer values for names are coming from). map(&:name) tells the form builder to use the tag's name instead.