Rails — Formtastic, how to set text for options in select? - ruby-on-rails

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

Related

How to change any by all in active admin collection data?

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.

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!

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 radio buttons and adding string to label

I am trying to set up a group of radio buttons in rails using simple_form. The options should come from a map.
In simple form, I saw there the :collection symbol can retrieve a collection (but only an array of arrays and not a hash).
How can I display a radio button collections with custom labels (ideally from a hash)?
Here is what I tried so far. Can I concatenate the symbol with an a string for a label?
#Payment.rb
OPTIONS = [[14, 19], [21,29], [30,39]] #ideally this would be a hash
#new.html.erb
<%= f.input :duration,
:collection => [ [14, 19.to_s], [21,29.to_s], [30,39.to_s] ],
:label_method => :last.concat(" days"),
:value_method => :first,
:as => :radio %>
if I understand your question correctly - you can use lambdas in :label_method. For example:
:label_method => lambda { |a| a.first.to_s + a.last.to_s }

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.

Resources