f.input :country_id, :prompt => 'Select Country', :as => :select, :collection => Country.all.map{|m| [m.name, m.id]} }
How to make one of the options from dropdown to be preselected. For example to select the value with id 5 what should be done?
Try this,
f.input :country_id, label: 'country', :as => :select, :collection => Country.all.map{|m| [m.name, m.id]},include_blank: false
just include 'include_blank:' parameter
Hope its work
Related
I have the following ActiveAdmin form:
form do |f|
f.inputs "Timesheet Details" do
f.input :jobs_assigned_worker, :label => "Worker", as: :select, collection: Worker.all
f.input :worked_time_hours, :label => "Worked Time (Hours)"
f.input :worked_time_mins, :label => "Worked Time (Minutes)"
f.input :driving_time_hours, :label => "Driving Time (Hours)"
f.input :driving_time_mins, :label => "Driving Time (Minutes)"
f.input :spent_dollars, :label => "Extra Money Spent"
end
f.actions
end
When I use this form in the edit view, the select drop-down automatically defaults to the present value. However in production the drop-down is for some reason defaulting to the blank value at the top (why is that blank value there anyway?).
EDIT
The problem seems to be that ActiveAdmin doesn't understand the association and is unable to select associated object by default. I need to figure out how to code the f.input for the association. The form is for a Timesheet. A Timesheet has_many JobsAssignedWorkers and each JobsAssignedWorker has a Worker.
If you want to include blank value:
f.input :jobs_assigned_worker,
label: 'Worker',
as: :select,
collection: -> { Worker.pluck(:name) },
include_blank: true
If you don't want to include blank value:
f.input :jobs_assigned_worker,
label: 'Worker',
as: :select,
collection: -> { Worker.pluck(:name) },
include_blank: false
If you want to have blank value, but don't want to allow it as an option:
f.input :jobs_assigned_worker,
label: 'Worker',
as: :select,
collection: -> { Worker.pluck(:name) },
include_blank: true,
allow_blank: false
Try to set 'include_blank' option.
form do |f|
f.inputs "Timesheet Details" do
f.input :jobs_assigned_worker, :label => "Worker", as: :select, collection: Worker.all, include_blank: false
f.input :worked_time_hours, :label => "Worked Time (Hours)"
f.input :worked_time_mins, :label => "Worked Time (Minutes)"
f.input :driving_time_hours, :label => "Driving Time (Hours)"
f.input :driving_time_mins, :label => "Driving Time (Minutes)"
f.input :spent_dollars, :label => "Extra Money Spent"
end
f.actions
end
to avoid persist the blank value just add in your select this option:
include_hidden: false
I have tried to use this on my test
<%= f.input :user, :label => 'Usuario: ' , :as => :select , :collection => #users , :include_blank => true %>
And didnt work....
But when a switch to:
<%= f.input :user_id, :label => 'Usuario: ' , :as => :select, :collection => Hash[#users.map{|b| [b.nickname,b.id]}] , :include_blank => true%>
It does work? Does anyone knows why?
I also put it all together on the code:
<%= f.input :user, :label => 'Usuario: ' , :as => :select , :collection => #users , :include_blank => true %>
<%= f.input :user_id, :label => 'Usuario: ' , :as => :select, :collection => Hash[#users.map{|b| [b.nickname,b.id]}] , :include_blank => true%>
But the first one didnt work (i did just to see if was somente wrong in the #user variable)..
Is something related to the model? Does anyone knows why?
From formtastic documentation: Many inputs provide a collection of options to choose from (like :select, :radio, :check_boxes, :boolean). In many cases, Formtastic can find choices through the model associations, but if you want to use your own set of choices, the :collection option is what you want. You can pass in an Array of objects, an array of Strings, a Hash…
You can check the documentation
also collection is expect to receive a hash or array , but when you pass #user you pass an instance variable of your model and that will not work.
I want to display the image on the gift on simple form association
I tried this code using label_method but it does not work:
<%=f.association :gift, :collection=> Gift.all, :label_method=>lambda { |gift| "#{gift.name} #{image_tag 'gift.photo.url(:small)'}" }, :value_method=> :id, :as=>:radio_buttons%>
This code can help you, show check boxes with images
<%= f.association :gift,
:as => :check_boxes,
item_wrapper_tag: :div,
item_wrapper_class: "image-select",
:collection => Gift.all,
:label => false,
:inline_label => false,
:include_blank => false,
:input_html => {multiple: true},
:label_method => lambda { |b| image_tag(b.photo.url) },
value_method: :id
%>
Don't forget to use html_safe if you are working with string
using simple_form we can change class of a label using:
label_html => {:class => "myclass"}
but how do we do the same when dealing with checkboxes?
simple_form assigns the default class of collection_check_boxes
Is there a way to change this default class?
I wanted to give an update to this answer in case someone comes here looking for a way to do this as I did.
You can give the label a class with this option :item_wrapper_class => 'class_goes_here'
Here is a full example:
= user.input :resident,
:collection => [["In the U.S", true],["Outside the U.S.", false]],
:label_method => :first,
:value_method => :last,
:as => :radio_buttons,
:label => "Where is your principle residence?",
:item_wrapper_class => 'inline'
If you want you can pass new_class to the label doing something like:
<%= f.collection_check_boxes attribute, collection, value_method, text_method do |b|
b.label(class: 'new_class') {b.check_box + b.text}
end %>
You should be able to set :input_html on your form input.
Somthing like:
f.input :something, :as => :check_box, :input_html => { :class => "myclass" }
ian.
The easiest way to change the label class for a checkbox is to insert the following in /config/inititializers/simple_form.rb or /config/initializers/simple_form_bootstrap.rb:
config.boolean_label_class = 'form-check-label'
This should be pretty straight forward like mentioned above one should add :label_html => { :class => "myclass" } in order achieve this, for instance:
= f.input :remember_me, as: :boolean, :input_html => { :class => 'kt-checkbox kt-mock-span' }, :label_html => { :class => "kt-login-checkbox-label" }
will create and assign the attributes for the styles on the checkbox's label like this:
To get the label class I had to get rid of the auto-generated label and write my own.
this is in rails 3 with simple form 2.1 so YMMV....
before:
<%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
after:
<%= f.label :remember_me, :class => 'remember-me' %>
<%= f.input :remember_me, :label => false, :as => :boolean if devise_mapping.rememberable? %>
I need to pass a collection to the standard select input in Formtastic:
f.input :apple, :as => :select, :collection => Apple.all
The problem is, though that I need Formtastic to access a different method than name. Now this is really a problem. I can always pass the Array
f.input :apple, :as => :select, :collection => Apple.map { |a| a.format_name }
The problem is, that after this I will get strings in the controller instead of IDs which is not desired. I tried to pass Hash instead:
options = Hash.new
Apple.each { |a| Apple.store(a.format_name, a.id) }
f.input :apple, :as => :select, :collection => options
Now the problem is, that since I am using Ruby 1.8.7, the Hash order is unspecified and I of course need ordered input...
I can imagine some solutions, but all of those require unnecessary code.
Any idea how to solve this right?
Try:
f.input :apple, :as => :select, :collection => Apple.all, :label_method => :format_name, :value_method => :id
There is no direct indication in the formtastic documentation, but collection can be nested arrays as well, so problem is solved by:
f.input :apple, :as => :select, :collection => Apple.map { |a| [ a.format_name, a.id ] }
This is the correct way now:
f.input :apple,
as: :select,
collection: Apple.pluck(:format_name, :id)
This sets collection to an array of [name, id] tuples. Easy!
Soon-to-be deprecated way:
Use the member_label option, e.g.
f.input :apple,
as: :select,
collection: Apple.all,
member_label: :format_name
Documentation is here in a code comment.