I'm trying to figure out how to use simple form without the pre-determined label.
I've tried setting label to false as set out below, but it doesn't stop the label of the attribute ('trl') from appearing beside the collection.
<%= f.input :trl do %>
<%= f.select :trl, Trl.all.map { |t| [t.title, t.id] }, label: false, include_blank: false, prompt: 'Select one' %>
<% end %>
Is there a way to dis-apply the labels in simple form?
try this:
<%= f.input :trl, label: false do %>
<%= f.select :trl, Trl.all.map { |t| [t.title, t.id] },include_blank: false, prompt: 'Select one' %>
<% end %>
Related
I have a enum definition for my User model.
class User < ActiveRecord::Base
enum program_of_study: [
:program_of_study_unknown, :program_of_study_cs, :program_of_study_ceg,
:program_of_study_is, :program_of_study_science,
:program_of_study_engineering, :program_of_study_fass,
:program_of_study_business, :program_of_study_others
]
end
And in simple_form I have:
<%= simple_form_for(locals[:user], wrapper: :horizontal_form, html: {class: 'form-horizontal'},
url: {action: (locals[:is_new] ? 'create' : 'update')}) do |f| %>
<%= f.error_notification %>
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: locals[:user].program_of_study %>
<%= f.button :submit, class: 'btn-success' %>
<% end %>
However, it seems that although the program_of_study of a user is 'program_of_study_science'(by checking in rails console), when I render the form, the shown select element still has 'program_of_study_unknown' as the displayed one. The correct one was not selected.
Instead of the enum I used the keys and it seems to work, have you tried that:
collection: User.program_of_studies.keys
I didn't have to specify a selected option. My code looks like this:
input :status, as: :select, collection: Venue.statuses.keys, include_blank: false
My solution in the end
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: User.program_of_studies[locals[:user].program_of_study] %>
I have a checkbox which doesn't have a label. How do I add a label tag in simple_form where the checkbox is inserted in the HTML label tag.
I currently have checkbox and plain text after it.
<%= f.check_box :is_positive, as: :boolean, checked_value: true, unchecked_value: false %> Is positive?
I tried the below code, but it makes the label higher than the actual checkbox.
<%= f.input :is_positive, as: :boolean, checked_value: true, unchecked_value: false, inline_label: 'Is positive?' %>
Is there a way to label the checkbox and have its label inline to the checkbox?
Please try the code below and you can use css to change the style of your label to whatever you like:
<%= f.label :is_positive do %>
<%= f.check_box :is_positive, as: :boolean, checked_value: true, unchecked_value: false %> <span class="labelText">Is positive?</span>
<% end %>
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.
Iam new to ruby on rails.I want to show the selected values from drop down and checked radio button in page reload.
When the page is reloaded, the selected value of the drop down and radio button are getting reset.So please let me know how to fix this issue.Here is the code i'am using.
<%= form_tag :action => 'show' do%>
<strong>Select device: </strong> <%= collection_select(:device, :id, #devices, :id, :name, options ={:prompt => "-Select a device"}) %>
<br></br>
<strong>Chose:</strong><%= radio_button_tag :name,:time, false, :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph%>Graph
<% end %>
Try:
collection_select(:device, :id, #devices, :id, :name, {:selected => #your_selected_value, :prompt => "-Select a device"})
see radio_button_tag(name, value, checked = false, options = {})
<%= radio_button_tag :name,:time, #your_checked_value.eql?('time'), :onclick => "this.parentNode.submit();"%>Time
<%= radio_button_tag :name,:graph, #your_checked_value.eql?('graph') %>Graph
How does one reset a Chosen plugin field after resource is created by Javascript?
I create a dog and can get the regular form fields to reset but not the select menu using the Chosen plugin:
dogs/create.js.erb
$('#dogs').prepend('<%= escape_javascript(render(#dog)) %>');
$('.add-dog-form > form')[0].reset();
This is successful reset, just not with the chosen menu which is called from my application.js:
jQuery( function($) {
// Chosen Select Menu
$('.category-select').chosen().trigger("liszt:updated");
});
Than my form:
<%= form_for(#dog, :remote => true) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<%= f.label :category, "Categories" %>
<%= f.select :category_ids, Category.all.collect {|c| [c.name, c.id]}, {}, { :multiple => true, :class => "category-select" } %>
<% end %>
So again, how does one reset a Chosen plugin field after resource is created by Javascript?
Try this:
dogs/create.js.erb
$('#dogs').prepend('<%= escape_javascript(render(#dog)) %>');
$('.add-dog-form > form')[0].reset();
$('.category-select').chosen().trigger("liszt:updated");
You basically have to trigger the reset again inside create.js.erb.