How can I get a hidden radio button using simple-form?
I know that simple-form has a :as => :hidden to hide a form field, and a :as => :radio_buttons to display a radio button option... but I can't have two as options on a field... :)
How can I go about this?
What I've tried:
= form.input :type,
:as => :radio_buttons,
:hidden => true,
:checked => ...
= form.input :type,
:as => [:radio_buttons, :hidden]
:checked => ...
= form.input :type,
:as => :radio_buttons,
:as => :hidden,
:checked => ...
"hidden" is a different form of input altogether - it's the same as hidden_field. If you just want to hide a radio from the user (maybe you want to show it again later) you can use "display: none" in the html for the wrapper.
= form.input :type
as: radio_buttons
wrapper_html: { style: "display: none" }
If the input should never be displayed to the user then I'd just leave it out of the page altogether. You're never going to be able to selected it after all.
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.
So I'm using nested_form (v0.3.2 according to Gemfile.lock) with Rails 3.2.11. I have a Service model with a category field that initially can take on multiple values that can be input via a select dropdown.
The categories are something like: ["J Award", "Z Award", "Other" ]
When I go to save the form fields with a value of J Award (or Z Award) and rerender an editable form, the form redisplays with a value of "Other" for category. Yet if I step into the rails console and look at the category field of the saved service model, it shows "J Award".
What could be causing this? Since nested_form is no longer maintained, should I just give up and handle multiple models in a single form differently?
haml output of rerendered form
= semantic_nested_form_for #service, :url => "/update", :html => { :class => "service", :autocomplete => "off" } do |f|
%h1.page-title Service
.page-wrapper
= render :partial => "shared/error_messages", :locals => { :object => #service }
html output of rerendered form:
<li class="string input required stringish" id="service_category_input"><label class=" label" for="service_category">Category<abbr title="required">*</abbr></label><input id="service_category" maxlength="255" name="service[category]" type="text" value="J Award" />
</li>
= f.inputs do
%h3 Project Information
= f.input :billable, :as => :radio, :collection => { 'Billable' => true, 'Non-billable' => false }, :label => 'Category', :input_html => { :disabled => true }
= f.input :category
= f.input :assigned_consultant, :input_html => { :readonly => true }
= f.input :aims, :input_html => { :readonly => true }
In your html.erb file, I'd change the f.input to f.select:
<%= f.select :category, ["J Award", "Z Award", "Other"], {selected: f.object.category} %>
The selected attribute is self-explanatory. I've never worked with .haml files before, so I guess you would have to convert it somehow.
Recently, I installed the formtastic-bootstrap gem on my ruby-on-rails application. I have the following code on one of my forms that includes check-boxes:
<% if #gallery.tag_list.empty? %>
<%= f.input :tag_list, :as => :check_boxes, :label => _('Select Applicable Tags'), :collection => Gallery::DEFAULT_TAGS.sort, :wrapper_html => {:class => 'horizontal_list'} %>
<% else %>
<%= f.input :tag_list, :as => :check_boxes, :label => _('Current Tags'), :collection => #gallery.tag_list.sort, :wrapper_html => {:class => 'horizontal_list'} %>
<% end -%>
Even though formtastic-bootstrap is rendering the rest of my form correctly, my checkboxes are not showing up. The text next to the check-boxes appears, but the checkboxed themselves do not. Any suggestions to get this working are welcome. I have been trying to circumvent this little, annoying issue all day.
I had to create a bootstrapcustom.css.scss to over-ride the bootstrap properties of padding and margins on check-box elements in order to make them appear in the end.
I am using gem 'simple_form', '2.0' and gem 'rails3-jquery-autocomplete', '1.0.10'.
I am unable to make a string field accept two ':as' arguments.
<%= f.input :product_description , :url => autocomplete_product_name_products_path,
:as => :text, :as => :autocomplete, :placeholder=>"Type product name",
:input_html => {:class =>"span2", :rows => 6}, wrapper: :inline_label, label:false %>
If I remove :as => :text, the auto-complete part works but ':rows => 6' part fail.
And if I keep both, I get 6 rows but the auto-complete stops working.
I need this field to have have multiple rows as well as auto-complete.
This is a really silly question but I am stuck. Please help.
Simple Form doesn't define an autocomplete input. But if you need an autocomplete input and the text type I think you can do:
f.input :product_description ,
:url => autocomplete_product_name_products_path,
:as => :autocomplete,
:placeholder=>"Type product name",
:input_html => {:class =>"span2", :rows => 6, type => :text },
wrapper: :inline_label, label: false
I've got phone_numbers which have 3 permissions of local, regional and national - each a boolean.
My form at the moment looks like this:
=semantic_form_for #phone_number, :html => {:class => "form-horizontal"} do |f|
=f.semantic_errors
=f.inputs do
=f.input :phone_number_type, :as => :select, :collection => PhoneNumber::PHONE_NUMBER_TYPES, :include_blank => false, :label => "Type", :required => false
=f.input :phone_number, :required => false
=f.semantic_fields_for :permission do |permission_fields|
=permission_fields.input :local, :label => false
=permission_fields.input :regional, :label => false
=permission_fields.input :national, :label => false
=f.actions do
=f.action :submit, :label => "Save"
Putting label false give each checkbox a single label to the right. Without it each checkbox has 2 labels - one on the left and one on the right. I've floated the 3 checkboxes, so they are in a horizonal line, so I'd really like local to have a label that says "Permissions" on the left and "local" on the right.
Is there a way to do this?
I just ended up hiding them with css