Make simple_form honor my blank/empty label - ruby-on-rails

Using simple_form v2.1.0 in a Rails 3.2.13 app. I have a simple_form where I want to have a checkbox show up with an inline label, and I want to have an "empty" normal label. This way, the label's container should remain in place and the checkbox will be properly positioned with the other form elements.
If I set :label => "" or :label => " " or :label => nil or don't provide a :label at all, I end up with the field name as the label.
If I set :label => false, the label and containing elements aren't rendered at all (so the checkbox appears at the far left of the page).
If I set :label => " ", I end up with the literal text , as expected.
It seems there is no way to tell simple form to leave the label there, just don't put any text in its container.
Here is an example form where I'm trying to setup an empty label for a checkbox with an inline label.
<%= simple_form_for #model ... %>
<%= f.input :sticky, :label => "", :inline_label => "Sticky?", :as => :boolean ... %>
<% end %>
But simple_form thinks it's smarter than me and defaults the label. I wish it only did that if I hadn't specifically supplied a label!

Try setting :label value to false.
UPDATE: Since SimpleForm is supposed to rely on ActionView to render the elements, you can use this hackish solution: set the label in your locale file to empty string, like this
en:
activerecord:
attributes:
model_name_underscored:
sticky: ' '

Have you tried removing the :label attribute in your input completely and instead using form.label for the empty label.
http://apidock.com/rails/ActionView/Helpers/FormHelper/label
Instead of using an empty label you could position your check box with CSS.

Was having similar troubles. In your form defaults just do this:
defaults: { :label => 'blanklabel', label_html: { class: 'blanklabel'}
Then in your CSS, just set the label text color to white:
.blanklabel { color: white; }

Related

Ruby on rails simple form text field value is overridden by default value

I am using a simple form like which has a text field with a default value. Once the user sets their desired value, the default value should be overridden by the user's desired value. But each time the user opens the form to edit it, they see the default value again and again:
<%= f.input :notes, input_html: {:value => #order_f.decorate.template_message, rows: 12} %>
Instead of setting a value, try setting a placeholder like so:
<%= f.input :notes_to_deliverer, placeholder: #order_f.decorate.deliverer_template_message, input_html: {rows: 12} %>
If you don't need the placeholder then you can do it like this:
<%= f.input :notes_to_deliverer, input_html: {:value => object.notes_to_deliverer.present? ? object.notes_to_deliverer : #order_f.decorate.deliverer_template_message, rows: 12} %>
Here the object is for which you have created the form. Also please make sure that this value is saved in database on form submit. And if suppose this your default value to be stored what I mean is if user doesn't enters any value then you need this to be stored then it would be better to use default value at database end. You can set it in migration file.
Hope this helps.

displaying formatted numbers in a rails simple_form view with angular js

I have a rails form created using the simple_form and haml with Angular JS code sprinkled in it. Angular is doing its job and computing the values fine. However, I have a display problem: I cannot display the totals being computed by angular and display it as a formatted value in a form control.
Here is the code snippet of the haml view:
= f.input :total_amount, :label => 'Invoice Total', :input_html => { :readonly => true, "ng-model" => "grand_total" }
So the form_control 'total_amount' is bound to the "grand_total" attribute on $scope for Angular.
Everything is working fine computationally. However, the total_amount input field is being displayed 'unformatted', e.g. 1655.3456. I would like to display it as currency, e.g., $1,655.00.
I do not know how to do it. I have tried to use the suggested approach of using number_to_currency rails view helper as shown below:
= f.input :total_amount, :label => 'Invoice Total', :input_html => { :readonly => true, "ng-model" => "grand_total", :value => number_to_currency(f.object.total_amount) }
But since the total_amount field is blank to begin with when I initialize the Rails view, it has no effect. Also, as the users change the nested form fields, the total amount is being updated dynamically by Angular. The number updates fine, but formatting is an issue.
Thanks in advance for any help.
You can use currency filter from angular https://docs.angularjs.org/api/ng/filter/currency
Because you are not using this field as input, try wrapping display value in something like span, then you can add filter like this
<span>{{total_amount | currency}}</span>
You can read about other options from the link
Or you can use directive from this answer https://stackoverflow.com/a/27051526/1805815

When using simple_form in Rails, how can I use it just to show data for some of the fields rather than have inputs?

I'm creating my forms using simple_form and it's all good, apart from when I want to just display some text rather than show an input box of some type. So I need to show a label and also the display text to go with it, e.g. Name : Chris, where "Name" is the label and "Chris" is the display text.
So Imagine I have a simple_form :
=simple_form_for #property do |f|
=f.display_field "Contact Name", "Chris"
=f.input :customer_reference
=f.input :premises_description
=f.input :po_number, :label=>"Purchase Order Number"
The "f.display_field" is a made up method, but it is how I imagine the method would look that I need. All it would do Is show a label and some text next to it. What is the easiest way to achieve this?
Cheers
Chris
I use a custom input for this purpose:
class FakeInput < SimpleForm::Inputs::Base
# This method usually returns input's html like <input ... />
# but in this case it returns just a value of the attribute.
def input
#builder.object.send(attribute_name)
end
end
If you place it somewhere like in app/inputs/fake_input.rb
you will be able to use it in your simple forms:
= simple_form_for #property do |f|
= f.input :contact_name, :as => :fake
The input's type is derived from the input's class name (without "Input", underscored).
So for FakeInput it is :fake.
To display some regular text instead of an input simply pass a block (and extra label in case it is no model attribute):
= simple_form_for #property do |f|
= f.input :contact_name, label: "Contact Name" do
Chris
= f.input :customer_reference
= f.input :premises_description
= f.input :po_number, :label => "Purchase Order Number"
This will simply output Chris at the same place where the input would be otherwise.
If I understand you correctly:
=simple_form_for #property do |f|
Contact Name:
=#property.contact_name
=f.input :customer_reference
=f.input :premises_description
=f.input :po_number, :label=>"Purchase Order Number"

How to set a field read only in rails 3.1.0 views?

My question is how to set a field in rails form read only. The following is a selection box in quotes controller. Users are not allowed to change the selection.
<% #quote.test_items.each do |t| %>
<%= f.association :test_items, :label => false, :selected => t.id %>
<% end %>
The app uses simple_form. Thanks so much.
I've encountered a similar problem, thankfully, there is a simple resolution.
The basic issue is that if you use :disabled => true with simple_form you will not see that value back in the controller. When you pass an object from HTML form to later bind it to the model - you need all of those attributes. The :disabled => true however does not pass any such attribute.
The solution to this is to use :readonly => true - it will protect the field from user entry and it will still pass the param value back to the controller so you can bind everything to your model.
Good luck.
See https://github.com/plataformatec/simple_form/pull/367
I believe you'd just pass in :disabled => true. It's been my experience that options 'just work' with simple_form. So in your case:
<% #quote.test_items.each do |t| %>
<%= f.association :test_items, :label => false, :disabled => true, :selected => t.id %>
<% end %>
From the simple_form github repo:
It is also possible to give the :disabled option to SimpleForm, and it'll automatically mark the wrapper as disabled with a css class, so you can style labels, hints and other components inside the wrapper as well.
Yes, what #gk0r said, as it is documented here:
NOTE: The HTML options disabled, readonly, and multiple can all be treated as booleans. So specifying :disabled => true will give disabled="disabled".
*disabled will have slightly different behavior than readonly.
The top answers above are all wrong.
disabled attribute has a different behaviour than readonly.
read and compare them:
http://www.w3schools.com/tags/att_input_disabled.asp
Tip: Disabled elements in a form will not be submitted.
http://www.w3schools.com/tags/att_input_readonly.asp
The right answer is to use
:readonly => true
something like this:
<%= f.association :test_items, :label => false, :readonly => true, :selected => t.id %>
It's not clear to me if the association method accepts HTML options or not, but if it does, you can pass disabled: 'disable' to make it read-only with a fixed value.
I think you might be able to choose the fixed value by passing association as block, as shown in the association docs:
f.association :company do |c|
c.input :name, selected: 'selection'
c.input :type
end
As to whether or not the entire list can be read-only and still drop-down, the only solutions I see from google involve JS, for example:
http://techeyes.blogspot.com/2007/11/making-html-select-readonly.html

Preselect Check box with Rails Simple_form

I'm using Simple_Form with Rails 3 and it's great. I have a simple question. I can create a check box using f.input if the type is boolean behind the scenes. However, I would like it to be preselected as true.
Is there a way to do this via the view?
If you don't want to, or cannot update your migration to do 'PerfectlyNormal's answer, then you can add the following to the end of your f.input:
:input_html => { :checked => true }
So, it would become:
= f.input :some_flag, :input_html => { :checked => true }
the best way would be to set the default value in your model, something like
create_table :my_table do |t|
...
t.boolean :my_boolean, :default => true
...
end
which simple_form will pick up on, and set the checkbox to be checked by default.
A simpler way is to do it as Dave says, and just force the value to 1, but if a validation fails, and you display the form again, it will still be checked, regardless of how it looked when the form was submitted.
It Worked for me which checks only one checkbox with value of "None"
<%= f.input :notify, label: "Notification", as: :check_boxes, collection: ["None", "Daily", "Weekly", "Monthly", "Quarterly", "Yearly"], :item_wrapper_class => 'inline', :checked => ["None", true] %>
I just stumbled over this post and what worked for me is similar to the latest answer - but shorter. Just initialize the Object with the wished setting:
def new
Object.new(my_boolean: true)
end
In my opinion, the rail way to do this would be to set the value of the attribute in the controller:
#some_object.some_flag = 1
This should work:
= f.input :some_flag, :input_html => { :value => '1' }
If you do the following, as in the highest rated answer (and as noted by #PerfectlyNormal) you are essentially hard-coding the state for every page load:
= f.input :some_flag, :input_html => { :checked => true } # don't do this
If you are using Rails-style server validation, this will have the unintended side effect of resetting the user's choice when validation fails.
Defaulting it in the model is ideal if the default value is static. In some cases though, the default value is driven by some business rule, so setting it at runtime is desired. For example, in my case, I wanted to default a checkbox to true, but only for certain types of organizations.
What you want is to default the value only when it is not already set, otherwise use the user-selected value. Here's how I handled this:
- is_checked = #org.is_special.nil? ? true : #org.is_special?
= f.input :some_flag, :input_html => { :checked => is_checked }
For Rails 6+, you can simply do:
<%= f.check_box :remember_me, checked: true %>

Resources