Formtastic - Why does not work - ruby-on-rails

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.

Related

How to make an option preselected in dropdown - activeadmin

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

how to change class of a label for checkboxes in simple_form

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? %>

Rails/Formtastic Newbie: Setting display field for referenced "select" box

New formtastic user here.
I have a relationship user has_many clients
In formtastic, if I do something such as
f.input :employer
it returns a select box of all employer object references. I'd like to display (last name, first name) instead. I'm sure this is very simple, but I can't figure out exactly how to do it.. Any help appreciated.
These didnt work for me, however, this did:
<%= f.input :user, :label => "Author", :label_method => :username %>
Also a little cleaner ^^
Or, you can set the display method once and for all on the model itself:
(In employer.rb)
def to_label
email
end
Try
f.input :employers, :as => :select, :collection => Employer.find(:all, :order => "last_name ASC")
or
f.input :employers, :as => :select, :collection => Employer.collect {|e| [e.last_name, e.id] }

How to properly pass collection for input in Formtastic

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.

Using :collection and :include_blank in Formtastic. How to do it?

I use the superb Formtastic plugin for Ruby on Rails.
Does anybody know how to include a blank (option), when using a custom collection?
When I try:
<%= f.input :organizations, :collection => Organization.all(:order => :name), :include_blank => true %>
I get the select box with the collection, but NOT a blank...
What kind of association is :organizations? Does it work if you specify :as => :select?
There's spec coverage for the following belongs_to select, date, time and datetime inputs:
f.input(:author, :as => :select, :include_blank => true)
f.input(:created_at, :as => :date, :include_blank => true)
f.input(:created_at, :as => :time, :include_blank => true)
f.input(:created_at, :as => :datetime, :include_blank => true)
My guess is that organizations is not a belongs_to association, right? If it's a :has_many or :has_and_belongs_to_many association, Formtastic will try to do checkboxes or a multi-select. In the case of a multi-select, obviously it makes no sense to have a blank line in there (you just don't select any of the items).
Hope this helps, please post some more details about the models and associations in question.

Resources