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
Related
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
I want to create a new pm_workload. I have the following code-snippet
<%= f.input :project_id, :collection => PmProject.order('name'), :label_method => :name, :value_method => :id, :label => "Project", :include_blank => false %>
Is there an option to use two values for :label_method? e.g.
:label_method => :name << (PmProject.project_number)
It's not possible to have to attributes or field name inside the label method. But I have an alternate soluction for you resolve this problem.
You are taking PmProject collections, So now create a instance level method insdie the PmProject model like as below.
def name_of_method
"#{name} #{project_number}"
end
Now to use the same on the view input of the simple form for use like that as below...
<%= f.input :project_id, :collection => PmProject.order('name'), :label_method => :name_of_method, :value_method => :id, :label => "Project", :include_blank => false %>
I've been looking at this problem this week. But a variation of it, in my case I wanted the label text to be wrapped in a <span>. I thought I was looking for was related to simple_form but actually it's still part of Rails ActionView tags helpers.
Yes, you could add another method on your model, but if you like to keep your models clean or you want to use something not in the model in the label. :label_method can also be a proc.
<% label_method = proc { |p| "#{p.name} #{p.project_number}" } %>
<%= f.input :project_id, collection: PmProject.order('name'), label_method: label_method, value_method :id, label: 'Project', include_blank: false %>
or in my case
<% label_method = proc { |p| "<span>#{p.name}</span>".html_safe } %>
<%= f.input :project_id, collection: PmProject.order('name'), label_method: label_method, value_method :id, label: 'Project', include_blank: 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'm using the below line of code for the date select in a form_for
<%= f.date_select :scheduled, {:prompt => {:day => 'Day', :month => 'Month', :year => 'Year'}}, :class => 'date-field', :include_blank => true %>
the date is optional in the form that it is being used. When the form is submitted the year and month field are defaulted to 1, which the api doc's explain will happen when a field is left blank.
below is the hash
"todo"=>{"name"=>"date tezt", "scheduled(1i)"=>"1", "scheduled(2i)"=>"1", "scheduled(3i)"=>"1", "scheduled(4i)"=>"", "scheduled(5i)"=>"", "assigned_to_id"=>""}, "commit"=>"Create To-do", "action"=>"create", "controller"=>"todos", "project_id"=>"1", "todolist_id"=>"1"}
How do you override this so that if nothing is entered the hash generated contains nil, for the year and month?
The issues was with how date_select and time_select play together.
I needed to add this option to time_select
:ignore_date => true
full code below
<%= form.date_select :scheduled, {:prompt => true}, :class => 'date-field' %>
<%= form.time_select :scheduled, {:minute_step => 15, :ignore_date => true, :ampm => true, :prompt => {:hour => 'Hour', :minute => 'Minutes'}}, :class => 'date-field' %>
Add , :include_blank => true Ref date_select
<%= f.date_select :scheduled, {:prompt => {:day => 'Day', :month => 'Month',
:year => 'Year'}}, :class => 'date-field', :include_blank => true %>
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? %>