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.
Related
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: " *";
}
In rails is there any simple way to implement select or create from view.
Eg:
Product has_many(or has_one) Tags.
While creating new Product I can select existing tags or create new one.
This can be done by using JavaScript and other ways are there.. But all will take more time and effort.
Please share if you know other simple way...
Edit:
Something like this.
But imagine you have 100 tags or more ! your page will look bad with 100 checkbox or more..., one elegant way to do this is by using a jQuery plugin called jQuery Tokeninput i use it in my project and it's very helpful for what do you want, you can find the plugin Here
This is a screencast on how to use it : Token fields
and this is the revised version : Token Fields (revised)
check also this blog post about the same plugin if you want too How to create a token input field where the user can also add new items
cheer
Yep.
You are after nested forms. Try, https://github.com/ryanb/nested_form
For example,
<% form_for #product do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<% f.fields_for :tags do |tag| %>
<p>
<%= tag.label :content, "Tag Name" %><br />
<%= tag.check_box :content %>
</p>
<%= tag.link_to_remove "Remove this tag" %>
<% end %>
<%= tag.link_to_add "Add new tag" %>
<p><%= f.submit "Submit" %></p>
<% end %>
Setup the controller and model as given in the documentation and try the above code in the view. This railscast will help you a lot in figuring nested forms http://railscasts.com/episodes/197-nested-model-form-part-2
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
I'm trying to customize (translate) an active record attribute name in rails 3.1 ("first_name"). Here's beginning of my locale file (config/locales/sv.yml):
"sv":
activerecord:
models:
employee: "Anställd"
attributes:
employee:
first_name: "Förnamn"
I'm sure this file is used by rails because changing translations further down in the file works.
Here's the form field erb code that should say "Förnamn" not "First name":
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
Running Employee.human_attribute_name(:first_name) in rails console returns "First name".
Thank you very much
In Rails 3.1 you can do it this way as well:
<% form_for #post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
en:
helpers:
label:
post:
title: 'Customized title'
This approach is ORM agnostic and works fine for active model (with mongoid for instance).
I realized that I had the activerecord entry defined two times in my sv.yml translation file which ment my attributes were overridden at the top of the file. It works now.
What is the difference between form_for and form_tag? Is anything different for form_remote_for and form_remote_tag?
You would use form_for for a specific model,
<% form_for #person do |f| %> # you can use f here
First name: <%= f.text_field :first_name %>
Last name : <%= f.text_field :last_name %>
<% end %>
Form_tag create basic form,
<%= form_tag '/person' do -%>
<%= text_field_tag "person", "first_name" %>
<% end -%>
form_for prefers, as its first arg, an activerecord object; it allows to easily make a create or edit form (to use it in a "new" view you should create an empty instance in controller, like:
def new
#foo = Foo.new
end
It also passes a form variable to the block, so that you don't have to repeat the model name within the form itself. it's the preferred way to write a model related form.
form_tag just creates a form tag (and of course silently prepare an antiforgery hidden field, like form_for); it's best used for non-model forms (I actually only use it for simple search forms or the like).
Similarly, form_remote_for and form_remote_tag are suited for model related forms and not model related forms respectively but, instead of ending in a standard http method (GET, POST...), they call an ajax method.
All this and far more are available for you to enjoy in the FormHelper and PrototypeHelper reference pages.
EDIT 2012-07-13
Prototype has been removed from rails long ago, and remote forms have completely changed. Please refer to the first link, with reguard to the :remote option of both form_for and form_tag.
These should be similar:
<% form_for #person do |f| %>
<%= f.text_field :name %>
<% end %>
and:
<%= form_tag '/person' do %>
<%= text_field_tag "person[name]" %>
<% end %>
If you want to submit the same params to the controller, you would have to define this explicitly.