I have an input which I render :as => :select. I would like one option to be selected as default. How would I go about that?
Thanks,
Hendrik
If you are not making a form off of a model.
= semantic_form_for :report do |f|
= f.inputs name: 'Choose a Report Year' do
= f.input :report_year, collection: options_for_select(['2010', '2011', '2012', '2013'], '2012')
= f.action :submit, label: 'Submit Year', as: :button
So set a preselected answer you have to associate your object with the desired value.
countries = ['USA','Germany']
#user.country = countries.first
That works for me.
You could read about this in the formtastic wiki. Always check gem readme before asking anything. Here is a link. Good luck!
If you don't have an associated model into the semantic_form_for you can do this:
f.inputs do
f.input :service_type, :as => :radio,
:collection => [
['Group', 'Group', { :'checked' => 'checked' }],
['Device', 'Device', {}]
]
end
Related
How can I add two label elements when choosing association in Simple Form on Ruby on Rails?
Sample: #user.name = "Barack" and
#user.last_name = "Obama"
Here is my code:
<%= f.association :persona, :collection => Persona.order(:name),
:prompt => 'Choose a person' %>
It displays only Barack but I need it to display not only name but also last_name when choosing from list.
<%= f.association :persona, :collection => Persona.order(:name), :label_method => lambda { |persona| "#{persona.name} #{persona.last_name}" }, :prompt => 'Choose a person'%>
Here is the answer - you need a complex label_method.
I have a rails 3 application which uses the ActiveAdmin gem.
Is there a way to make a form in the dashboard page. A form that is not related to any models ? In my case, a form to select time period to display some stats ?
I have tried :
form do |f|
f.inputs "test" do
f.input :time, label: "Duration", as: :select, collection: [['24h', 24], ['1 week', '1w'], ['1 month', '1m']]
end
f.actions
end
But i'm getting an "undefined methods errors" for f.inputs. Does someone have an idea ?
You probably need to create a form partial like this:
-# admin/whatever/_form.html.haml
= semantic_form_for 'whatever', :url => admin_whatevers_path do |f|
= f.inputs :name => 'tests' do %>
= f.input :time, collection: [['24h', 24], ['1 week', '1w'], ['1 month', '1m']]
and tell activeadmin to use the partial like this:
#admin/whatever.rb
form :partial => 'form'
I have a two select fields and their default options are blank so I would like to set all of them to 'Choose one'. I can do it for individual fields but I want to do it in the config somehow (avoiding the redundancy).
form do |f|
f.inputs "Item" do
f.input :field_1, :prompt => 'Choose one', :foo
f.input :field_2, :prompt => 'Choose one', :bar
end
end
How do I do this? :)
Edit: These prompts would be used on many forms. It really needs to be a config thing.
Try using a array:
fields = [:field_1, :field2]
form do |f|
f.inputs "Item" do
fields.each { |field| f.input field, :prompt => 'Choose one' }
end
end
I am using simple-form in rails and I would like to know if there is a way to change the way collection objects are displayed. For example I have something like this
<%= f.input :lang, :label => "Language", :collection => [ "en", "es, "zh", "fr" ] %>
Instead of showing up as "en es zh" etc I would like it to show up as "English Spanish" etc to the user. Is there anyway to do that sort of thing?
Thanks in advance.
You can use following way as well:
In Model:
LANGUAGES = {'English' => 'en','French' => 'fr'}
In View:
<%= f.input :lang, :label => "Language", :collection => Model::LANGUAGES %>
I guess that the label will be "English" and the value "en"; you can do something like this:
Store the data in a Model (recommended) or make a hash:
#data = Language.all
In the view use label_method and value_method:
<%= f.input :lang, :label => "Language", :collection => #data, :label_method => :name, :value_method => :value %>
Check the section Collections in https://github.com/plataformatec/simple_form
Another option is to add a helper
def languages_display
[
["English", "en"],
["Spanish", "sn"],
["French", "fr"],
]
end
And then call your helper from the input field:
<%= f.input :state, :collection => languages_display %>
And then on your show view you could call the following helper so that it displays English and not the en you have in the db:
def show_language(language)
{
"en" => 'English',
"sp" => 'Spanish',
"fr" => 'French'
}[language]
end
<%= f.input :lang, label: "Language", collection: [ [ "English", "en" ], [ "Spanish", "es" ], [ "French", "fr" ] ] %>
This works above, use a nested array. Also, this was used in the latest rails and simple form.
My approach is to put entire collection in locale yml file (en.yml):
#RAILSROOT/locales/en.yml
en:
collections:
languages:
- - en
- English
- - de
- Deutch
and in view just write:
<%= f.input :lang, :label => "Language", :collection => t("collections.languages") %>
I use this A LOT so I even wrote gem with helper functions which extends I18n (https://github.com/rstgroup/i18n-structure) and with that in gemfile you can write (notice "tc" helper)
<%= f.input :lang, :label => "Language", :collection => tc(:languages) %>
I have this piece of code, while using simple_form:
= simple_form_for :report do |f|
= f.association :presets,
:collection => #account.presets.collect{ |p| [p.name, p.id] },
:as => :check_boxes
How can I preselect a specific preset checkbox, knowing that the ID of this preset is passed within params[:preset_id]? The checkboxes' HTML name attributes are report[preset_ids][].
According to the simple_form documentation:
The association helper just invokes
input under the hood, so all options
available to :select, :radio and
:check_boxes are also available to
association. Additionally, you can
specify the collection by hand, all
together with the prompt:
f.association :company, :collection
=> Company.active.all(:order => 'name'), :prompt => "Choose a Company"
So, you should use something like this:
= simple_form_for :report do |f|
= f.association :presets,
:collection => #account.presets.collect{ |p| [p.name, p.id] },
:as => :check_boxes,
:checked => params[:preset_id]
I don't have experience with simple_form, but this might help :)
An update for everybody. the :selected option did not work for me. I used:
:checked => [2, 3]
Hope it helps someone.
f.association Really did the trick, thanks :), for preselecting, saving, and everything, I don't have reputation enough to vote up your answer (#claudio-acciaresi) that's why I'm commenting here...
This is my snippet:
<%= f.association :association, collection: Model.all,
value_method: :id, label_method: :name,
as: :check_boxes, include_blank: false %>
Replace symbol :association with the current has_many from the model.
Replace Model.all, for your source data.
Hope it gets useful for someone else :)
Regards.
Don't forget to cast params[:preset_id] to integer:
params[:preset_id].to_i