How to change any by all in active admin collection data? - ruby-on-rails

I am using collection for filter data.
filter :"status" , :as => :select, :collection => User::STATUS
I want to change default option label any by all.
How to do it in active admin?

you can use option prompt
ex,
filter :"status" , :as => :select, :collection => User::STATUS, prompt: 'All'
Hope it will Help you.

Related

RubyOnRails - option_groups_from_collection_for_select and translation of content

I have a collection, I am showing to the user.
But I need to I18n.translate the :key into a readable text from my de.yml.
competences:
key:
compkey001: "Werbung / 360"
compkey001subkey002: "Klassische Werbung / ATL"
compkey002: "Strategie"
f.input :competence_id, label: "Kompetenz", :as => :select,
:collection => option_groups_from_collection_for_select(
#competence_kategories, :competence_unterkats, t(:key,
scope: 'basic_data.competences'), :id, t(:key,
scope: 'basic_data.competences'))
This is not working.
It is giving me an error, something like: t() is not a method.
The below line is working, but showing the keys that are not usable by the user:
f.input :competence_id, label: "Kompetenz", :as => :select,
:collection => option_groups_from_collection_for_select(
#competence_kategories, :competence_unterkats, :key, :id, :key)
Displayed will be:
(bold)compkey001
compkey001subkey001
(bold)compkey002
... etc.
How can I get the translations being displayed in a grouped manner?
Thanks for help!

Active Admin Filters no viewing

So I'm in Active Admin and in the following model a Degree belongs to a user and a User has many degrees
For a User we have a site_id that identifies where the User works in a Site table
So in my filter in Active Admin's degree model, I can't seem to pull a list of sites that someone can filter from in the Active Admin UI.
Here are my filters so far:
major
institution
completion_date
These are straight from the Degree tables
:user_active comes from Users table with a boolean attribute for an an Active
ActiveAdmin.register Degree do
belongs_to :user, :optional => true
menu :parent => 'Users'
config.sort_order = 'users.last_name_asc'
filter :user_active, :as => :select
**filter :site_id, collection: -> { User.all }, label: 'sites'**
filter :degree_type
filter :major
filter :institution
filter :completion_date
I've tried this as well
filter :site_id, :as => :select, collection: -> { User.all }, label: 'sites'
and no error message
I've also tried something like this with no error message, but nothing in the UI comes through again
filter :site, label: "Site", :as => :select, :collection => User.site_id
I've tried this and get the following errors:
filter :site_id, label: "Site", :as => :select, :collection => User.all
undefined method `site_id_eq' for Ransack::Search<class: Degree, base: Grouping <combinator: and>>:Ransack::Search
Any help here?
Update
I did some work-around and this is what I end-ed up with
filter :user_site_id, label: "site", :as => :select, :collection => User.all.map{|u| u.site}.map{|s| s.city if s.present? }.uniq.compact
The collection says
Iterated through all the users and call u.site (because a site_id
belongs to a User so there's a Site table that'll take each instance
of a user)
Since we're now at each Site object, map each of those
values and retrieve the city name for each Site object if it's
present...
I want to only filter the uniq values and .compact is
getting rid of the nil values in the array
I think there's a better solution because I'm going through so many iterations? Anyone have any other ideas? I'm going to make this an instance method on the Users model after the refactor.
Try this out:
filter :site_id, as: :select, collection: -> { User.pluck(:site_id) }, label: 'Sites'

Create a drop down menu using a Table Attribute in Rails Active Admin

I need to create a dropdown field(membership_code), whose values are contained on a different table called members.
Schema
prereg
id
membership_code(string) not a foreign key
verification_code
members
id
membership_code
Prereg Active Admin Model
ActiveAdmin.register Prereg do
form do |f|
f.inputs "Preregistered Users" do
f.input :verification_code
f.input :email
#THIS LINE NEEDS TO BE CHANGED TO LIST DOWN THE MEMBERSHIP_CODE FROM MEMBERS
# f.input :membership_code, :as => :select, :collection => Members.all()
end
f.actions
end
To add, I was planning to have this logic wherein whenever you create a Prereg record, the selected "membership_code" would be deleted from the members.membership_code list.
How is this done in ActiveAdmin? Sorry I haven't found any good resource for DB Hooks and I'm still new to Rails.
I think you are looking for something as follows:
f.input :membership_code, as: :select, collection: Member.all.map(&:membership_code)
try this
f.input :membership_code, :as => :select, :collection => Members.select(:membership_code)
Thanks

Rails Simple-Form group_method

I came across the following on the simple_form github repo:
f.input :country_id, :collection => #continents, :as => :grouped_select, :group_method => :countries
The thing that caught my attention was the :group_method wich would be exceptionally usefull when creating a selectbox that gives options based on what's in the database. The only thing I can't work out is what kind of input the :group_method expects, and where to put the method.
For instance, I want to create a selectbox for the table column :product_type. I imagine I would write something like this in my simple form:
= f.input :product_type_contains, :collection => #products, :as => :grouped_select, :group_method => :product_types
where :product_type would be the method that is being called. But I don't know what kind of method I should write, what kind of result simple_form expects, and if I should put it in the Product class, Product.rb. Any help would be greatly appreciated!
According to the test suite, simple_form seems to expect the type of arrays or hashes that you would use with grouped_options_for_select:
test 'grouped collection accepts group_label_method option' do
with_input_for #user, :tag_ids, :grouped_select,
:collection => { ['Jose', 'Carlos'] => 'Authors' },
:group_method => :first,
:group_label_method => :last
[...]
test 'grouped collection accepts label and value methods options' do
with_input_for #user, :tag_ids, :grouped_select,
:collection => { 'Authors' => ['Jose', 'Carlos'] },
:group_method => :last,
:label_method => :upcase,
:value_method => :downcase
[...]
Presumably, you could write a class method on Product.rb that creates a structure similar, or even try using grouped_options_for_select(#products)...
Hope this gets you on the right path.

Rails — Formtastic, how to set text for options in select?

f.input :some_model_values, :as => :select
Using Formtastic, I need to set a text for options on my select input, but not populated from :some_model_values.
The second:
how could be collection of :some_model_values pre-modefied? Because i don want to show some options according to user role.
Thanks!
What do You mean with "set a text for option on my select input" ?
Please have a look at the :collection parameter, for example:
f.input :some_model_values, :as => :select, :collection => [1,2,3,4]
or
f.input :some_model_values, :as => :select, :collection => [["male",1], ["female",2], ["other",3]]
When you want to set the label, the best option is to localise it by the /config/locales/#locale#.rb file
Hope this helps

Resources