I'm using simple form, and I have the :selected => 0 so that the select has value 0 from its collection by default. But this is not working, the select box is blank and I need to choose the "0" option manually... I'm in a modal by the way. Any idea?
<%= f.input :price_type, label: "¿Conoces el precio?", collection: ["0","1","2"], :selected => 0 , id: "tag_price_type", html_input: "required", input_html: {class: 'ipt'} %>
Try matching the object types between the collection and the selection. So either:
<%= f.input :price_type, label: "¿Conoces el precio?", collection: [0,1,2], selected: 0 , id: "tag_price_type", html_input: "required", input_html: {class: 'ipt'} %>
or
<%= f.input :price_type, label: "¿Conoces el precio?", collection: ['0','1','2'], selected: '0' , id: "tag_price_type", html_input: "required", input_html: {class: 'ipt'} %>
You have to use quotes, see: How do I set the default selected item in a Rails drop-down menu?
So you should have: :selected => "0"
Related
I am trying to remove the label that is auto included when we use '.association' of the simple_form_for. But, regardless of what I do, the title and its <hr> continue to be displayed.
I tried:
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label: false %>
and
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label: "" %>
But it keeps showing :/
What can I do to remove it?
You can try this:
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label_method: "" %>
I am using simple_form in my rails application. I want to disable a particular value in drop-down.
Here is part of the code
= simple_form_for(#organization,url: admin_organization_path) do |f|
= f.input :hospital_name, input_html: { class: "form-control"}
= f.input :parent, collection: #organizations, input_html: { class: "form-control", id: "chosen-select-speciality"}
I tried using :disabled => #organizations.first but i was failed.
Is there any other method to use.Kindly help me. thanks.
Simpleform builder for select box uses value of the each option to compare with value of disabled attribute, so you just simple have to use id of the organization to disable desired option:
= simple_form_for(#organization,url: admin_organization_path) do |f|
= f.input :hospital_name, input_html: { class: "form-control"}
= f.input :parent, collection: #organizations, input_html: { class: "form-control", id: "chosen-select-speciality"}, disabled: #organizations.first.id
If you want to disable several options for selectbox, you can manually build options list inline or using helper and use it as attribute for input:
= f.input :parent, collection: #organizations.map{|o| [o.id, o.name, {disabled: o.id.in?([1,21,10])}]}, input_html: { class: "form-control", id: "chosen-select-speciality"}
By using JavaScript you can easily disable a particular value in drop-down as:
$(/* option selector */).prop('disabled', true);
See it in action
To disable the particular value use below code:
<%= f.input :parent,
collection: #organizations,
input_html: { class: "form-control",
id: "chosen-select-speciality"},
disabled: #organizations.first.id %>
If you want to disable multiple options, put below code to fetch the list of unused organizations in your helper file:
#unused_org_list = Organization.where("Condition to fetch unused organizations")
Then, in your view file:
<%= f.input :parent,
collection: #organizations,
input_html: { class: "form-control",
id: "chosen-select-speciality"},
disabled: #unused_org_list&.map {|u| u.id} %>
NOTE: Make sure to pass ids into disabled option as it expect id or [array of ids]
I'm using the gem simple_form and trying to do a ".mb_chars" on my field. Any idea?
My field:
<%= f.input :metadesc_mb, label: 'Meta description' %>
Maybe something like this (don't work):
<%= f.input :metadesc_mb.mb_chars, label: 'Meta description' %>
Thanks in advance.
I believe you should use :value_method key to pass the string processed with #mb_chars. That will
something like the following:
<%= f.input :metadesc_mb, label: 'Meta description', value_method: -> { self.metadesc_mb.mb_chars } -%>
or to use :value subkey of :input_html key:
<%= f.input :metadesc_mb, label: 'Meta description', input_html: { value: self.metadesc_mb.mb_chars } -%>
NOTE: The last method can't be applied to collections.
What is the best way to place two radio button inside a simple_form, where
one button is by default disabled and another is selected!
f.input_field :listing_type, as: :radio_buttons, collection: [ "lease", "sale"], :item_wrapper_class => 'inline'
this is giving me two buttons where both are enabled and selected. I want sale to be disabled and lease to be selected by default.
Here's an updated answer. You can:
Specify which radio button is disabled by adding hash to the collection: collection: [['op1', 'val1', disabled: false], ['op2', 'val2', disabled: true]]
Specify which radio button is checked by either add checked to its collection hash, or add checked: value_to_be_checked to the main options hash.
Here's an examples of both options:
example1:
<%= f.input_field :listing_type, as: :radio_buttons, collection: [ ['lease', 'lease', disabled: false], ['sale', 'sale', disabled: true]], checked: 'lease', :item_wrapper_class => 'inline' %>
example2:
<%= f.input_field :listing_type, as: :radio_buttons, collection: [ ['lease', 'lease', disabled: false, checked: true], ['sale', 'sale', disabled: true]], :item_wrapper_class => 'inline' %>
Then you have to make them separately with the same name so that one of them can be disabled.
<%= f.input :listing_type, as: :radio_buttons, :disabled: true, input_html: { value: 'lease' } %>
<%= f.input :listing_type, as: :radio_buttons, input_html: { value: 'sale' } %>
Radio Buttons behave the same as Select Options as far as I can tell. If you pass an array to an input's collection option consisting of the Display Text, the value of the input, and any additional HTML attributes - in that order - you can embed whatever you want in the radio buttons (or any inputs for that matter).
Ex:
radio_button_options = LeaseCalculator::TERMS.map do |term|
["#{term} Months",term, {disabled: (term > maximum_term_length)}]
end
= form.input :term,
as: :radio,
collection: radio_button_options
Here I put a conditional on the HTML5 disabled attribute. It works perfectly in the resultant HTML.
<input id="lease_calculator_term_48" data-calculator-input="term" class="update_calculator" type="radio" value="48" name="lease_calculator[term]">
<input id="lease_calculator_term_60" data-calculator-input="term" class="update_calculator" disabled="disabled" type="radio" value="60" checked="checked" name="lease_calculator[term]">
NOTE: The second input has HTML5 Disabled on it, the first one does not.
try this out
f.input_field :listing_type, as: :radio_buttons, collection: [ "lease", "sale"], :item_wrapper_class => 'inline', :checked => "lease"
you can just hack radio_button disabled functionality with jquery insist of separating out radio buttons.
f.input_field :listing_type, as: :radio_buttons, collection: [ "lease", "sale"], :item_wrapper_class => 'inline', :checked=> "lease", :disabled=> true
/app/assets/application.js
$(':radio:not(:checked)').attr('disabled', true);
See you can use the form helper to make your form and set the default value like this
<% form_tag desired_path do %>
<%= radio_button_tag(:options, "res_a", true) %>
<%= label_tag "res a" %>
<%= radio_button_tag(:options, "res_b") %>
<%= label_tag "res_b" %>
<%= submit_tag :value => "submit"%>
<% end %>
by providing true you can make the option as default as shown.
I'm trying to set a label for the following line but I keep getting an error if I add label_method anywhere. label: is just ignored. How can I add a label for f.select?
<%= f.select :state_identifier, Location::STATE, { prompt: 'State', id: 'state' } %>
I tried the following but it doesn't format properly in form-horizontal, leaving no gap between the label and data.
<%= f.label :state_identifier, label: 'State' %>
This is because f.select is not a simple_form method and does not support :label
Something like this should work for you w/ simple form.
<%= f.input :state_identifier, :label => "State", :collection => ["a","b"], :input_html => {:id=>"state" } %>
Hope this helps.