Rails Simple-Form group_method - ruby-on-rails

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.

Related

populate drop down list from array in ruby

I have a country list that I have called in - and have parsed into the following format:
{"Countries"=>[{"Name"=>"ABKHAZIA", "IsoCode"=>"AB", "HasTown"=>"I"}, {"Name"=>"ANGUILLA", "IsoCode"=>"AI", "HasTown"=>"I"}, {"Name"=>"ANTIGUA", "IsoCode"=>"AG", "HasTown"=>"I"}, .... {"Name"=>"ZIMBABWE", "IsoCode"=>"ZW", "HasTown"=>"I"}]}
I want to populate a drop down list with this data. Code I am using to create the drop down box is:
def country_selection_input options = {}
options.reverse_merge!(
:attribute => :country_iso,
:collection => transaction_form.get_countries,
:input_html => {},
:prompt => 'please select',
:label => 'To Where'
)
This gives me a drop down box with a please select prompt and a list that consists of only the one word: Countries.
The data is there - but I am not sure how to get it into the drop down list - and am sure I am missing something simple.
I have tried
:label_method => :Name,
but get an error message of
undefined method `Name' for #<Array:0x007fc385cecbb0>
This will probably turn into a menu as I want to take action based on the country selected - but - this is the first step - getting the list to work.
The answer ended up being
def country_selection_input options = {}
countries = transaction_form.get_countries()[:Countries]
options.reverse_merge!(
:attribute => :country_iso,
:collection => countries,
:label_method => :Name,
:value_method => :IsoCode,
:input_html => {},
:prompt => 'please select',
:label => 'To Where'
)
call_input_from_args_hash options
end

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!

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 3 Model.all alternative?

I have a number of Model.all queries in collections in my view (using them for drop down lists to populate a form). Some of the lists returned have many records and it's taking a long time to load the edit view as a result.
Is there an alternative to calling .all in the following?
= f.input :descriptivedetail_primarycontenttype, :label => "Content type", :as => :select, :collection => Contenttype.all, :value_method => :code
I can't use a scope as I get the error
undefined method 'first' for :allisbns:Symbol
where :allisbns is
scope :allisbns, Isbn.all
in the model.
Thanks!

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