Why does f.label transform 'ALL' caps string to 'All' caps - ruby-on-rails

I have an object that whose values are all caps, and I would like the f.label helper to print it out as all caps - without having to do a CSS transform.
Right now, if I have the string AAPL in my object, f.label spits it out as Aapl.
The reason I don't want to use a CSS transform is because the value of the object will not ALWAYS need to be all uppercase. I just want the f.label to output it exactly as it is stored in the db - preferably without any CSS shenanigans.
How do I do that?

It sounds like something is calling humanize(). This seems to happen, when Rails translates an element name to a label title. Probably you should define the text for the label explicit to it's form element.
<%= f.label(:aapl, 'AAPL') %>
or even
<%= f.label(:aapl, #yourVar) %>
Another option for you might be the translation ability, which Rails provides. This is valid for Rails > 3.1!
In your view:
<% form_for #post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
In your en.yml:
en:
helpers:
label:
post:
title: 'Your Title'

Try this:
f.label(topic).upcase.html_safe

Related

rails - how to translate what is within a ruby code in a form format?

I have a ruby code.
<%= f.label "Email *" %>
And I want to translate the English word "Email" to Japanese word because I am currently internationalizing my website written in English.
I tried the following.
<%= f.label "<%= t(:email) %> *" %>
However, it did not work. What should i do?
You should not use <% %> within another <% %>. Write it like this:
<%= f.label "#{t(:email)} *" %>
The <% tag already switches to ruby, where = also will render the outcome of the code. In order to get a translation, you might want to use <%= f.label t("email") %> and use email in your translation files.
As you need a translation inside a form, if it is bound to a model you can also translate your model as described inside the translate your models section inside the guides. Then <%= f.label :email %> will translate out of the box.
There are several ways to mark required fields, you may simply do <%= f.label "#{t("email")} *" %> or style the required field via css, like:
# app/view/.../....rb
<%= f.label :email, class: 'required' %>
# app/assets/stylesheets/.../....css
label.required:after {
content: " *";
}

Get data attribute value from the Rails form builder without using input field

I have what I hope to be a simple question. I need to display the value for an attribute on the Edit page, while keeping the input field for the same attribute. How might this be accomplished?
Well generally you can just use the original object, like you'll have an #foo that you'll have used in your form_for statement, so you can just use that directly: = #foo.the_attribute
If you're within a partial, or elsewhere where you have only the form builder instance, then you can refer to the underlying object with the .object method, eg.:
= form_for #foo do |f|
# in here, f.object == #foo
In my case, I'm working with accepts_nested_attributes_for in two models. Event accept nested objects from Speaker. And Speaker has a perfil_id attribute which could be ['Maker', 'Developer', 'Entrepreneur', ...].
The Speaker's form is a partial rendered from the principal form, Event's form:
<%= form_for(event) do |f| %>
<%= f.text_field :title %>
<%= f.label :title, 'Event name' %>
<%= f.fields_for :speakers do |builder| %>
<%= render 'events/partials/speaker_fields', f: builder %>
<% end %>
<%= f.submit %>
<% end %>
Partial
<%= builder.number_field :name %>
<%= builder.label :name %>
<% options = options_from_collection_for_select(#profiles, 'id', 'name', f.object.member_profile_id ) %>
<%= select_tag "event[speakers_attributes][profile_id]", options, prompt: 'Select a Profile' %>
When editing Event's Speakers I wanted a select_tag to select the profile name for the actual Speaker.
I could not use an input field for this. So I need to get the correct values from the builder object and I get what I need by doing this:
f.object.profile_id
Passing it as a fourth param to the select options I get this working:
<% options = options_from_collection_for_select(#profiles, 'id', 'name', f.object.profile_id ) %>
I hope it could be useful for you too!

How does one hide or show a form input, based on the value of another input?

How can I dynamically hide an input field that is built using Rails form helpers? For example, consider the following code:
<%= f.select :notification_type, [['Specific','Specific'],['Mass','Mass'],['All','All']] %>
<%= f.label 'Enter the specific customer imei whom you want to send' %>
<%= f.text_area :imei %>
How would I hide or show the imei text area based on the value of notification_type?
You would need to use JS/jQuery if you want this to happen without refreshing the page. First set your form like so:
<%= f.select :notification_type, [['Specific','Specific'],['Mass','Mass'],['All','All']], class: "notification-type" %>
<%= f.label 'Enter the specific customer imei whom you want to send' %>
<%= f.text_area :imei, class: "imei", display: none %>
Then add some jQuery (CoffeeScript in this example) either in your assets/javascript or your views/action_name.coffee.erb like so:
$->
$(".notification-type").on "select", ->
if $(this).val() == "Mass" # replace the condition with the thing that should show the text area
$(".imei").show()
else
$(".imei").hide()
Something like this should work.. though you'll probably need to play with it as I haven't tested this code.

form for helper bad collection

I am useing a form_for helper to collect data on the client side of my application. However something weird is happening. I am not collecting the :name and :description and they are both returning as nil. this is my code:
<%= form_for #type do |f| %>
....
<%= f.text_field :name, :class => "col-xs-4" %>
<%= f.text_field :description, :class => "col-xs-4" %>
<%= f.submit %>
....
Do I need to make a fields_for under the form_for to get this working? It is a bit tricky because I am using #type which in this case is set up to tell the view which kind of attr. they are looking at. For example, this line:
<%= f.label #type %> <label> Description</label>
depending on what view you are on shows ether:
Group Description
or
Tag Description
and because they are both technically the same, I am using the same index for both. I hope I am clear with my issue and thank anyone who understands my problem and solution.
The param name will depend on the object you pass.
If #type contains an instance of Group, then you will get the params under params[:group], and if it is an instance of Tag, the you will get them on params[:tag]
<%= form_for #type do |f| %>
<%= f.label :name, "#{#type.model_name} Description" %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
Note the label definition. The way you are defining it will create 2 labels and the second one will not be linked to any field.
fields_for is normally used when you are creating several objects within the same form, for instance a Project and several tasks associated to it.
Hope this helps.
update:
If #type is a string or symbol it should work too. The tradeoffs using this approach will be that if there are any validation errors when creating the object, those will not be displayed and the fields will not be prefilled with the input that the user gave before submitting the form, forcing the user to enter all the information again and guessing which was the validation error (you can initialize it from the received params, but that complicates the code readability)
The unique thing different in your view would be the label definition.
<%= f.label :name, "#{#type} Description" %>

Rails i18n and yml structure for form labels

According to the ActionView documentation. Quote:
The text of label will default to the attribute name unless a translation is found in the current I18n locale (through views.labels.<modelname>.<attribute>) or you specify it explicitly.
I have a "user" model and a registration form. Here's a snippet of the relevant part:
<% form_for(#user) do |f| %>
...
<p>
<%= f.label :username %>
<%= f.text_field :username, :class => 'full_width' %>
</p>
...
<% end %>
Dots hide unimportant code.
As I understand the documentation, if I provide a translation in my locale file, in this case :dk, my dk.yml looking like so:
dk:
views:
labels:
user:
username:
"blahblah"
Rails should translate the label text and insert "blahblah" instead of "Username".
This is not happening, so I must have missed something. Any help appreciated.
In Rails 3.1 that is a little bit changed.
<% form_for #post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
en:
helpers:
label:
post:
title: 'Customized title'
I think I found another solution here.
My app was version 2.3.5. I've now changed it to 2.3.8 and <%= f.label :username %> now uses the translation in:
dk:
activerecord:
attributes:
user:
username:
I found the hint in this ticket:
https://rails.lighthouseapp.com/projects/8994/tickets/745-form-label-should-use-i18n
That's because the label method you are calling is not the one from ActionView::Helpers::FormHelper but is in fact the label_tag method from ActionView::Helpers::FormTagHelper. The form_for method is rewriting the code in the given block by adding _tag to the used form helpers. So you're not looking at the documentation for the right method!
I've not yet used that method, as sometimes the label for a field can be different from multiple forms using the same model, so I've written my own helper.

Resources