I want to translate a rails form using i18n system.
My model attributes are translated correctly, but when I want to translate submit actions, the model name is not translated.
Here is my fr.yml locale file
fr:
activerecord:
model:
character:
one: "Personnage"
other: "Personnages"
attributes:
character:
name: "Nom"
title: "Titre"
biography: "Biographie"
helpers:
submit:
update: "Mise à jour %{model}"
My _form.html.erb is
<%= form_for(#character) do |f| %>
<% if #character.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#character.errors.count, "error") %> prohibited this character from being saved:</h2>
<ul>
<% #character.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :biography %><br />
<%= f.text_area :biography, :rows => 8%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
On my form, I expected the update button to be "Mise à jour Personnage", but I still have "Mise à jour Character".
Thanks for your help !
Looks like you have a model where you should have models.
Change your fr.yml to look like this:
fr:
activerecord:
models:
character:
one: "Personnage"
other: "Personnages"
...
See the rails i18n documentation section on Translations for Active Record Models for details.
This is the default behavior of the submit button when used in the form_for tag...it will show the Update Character instead of "Update #{Model.name}"...
Related
I'm using the rails-i18n gem to use :hr for my main language.
The gem works but for header message it doesn't.
(The 4 errors prohibited this list from being saved: part)
This is what I get when I submit the form with invalid atributes:
4 errors prohibited this list from being saved: #doesn't translate
Field1 ne smije biti prazan #translates/presence
Field2 ne smije biti prazan #translates/presence
Field3 ne smije biti prazan #translates/presence
Filed4 nije odgovarajuće duljine (treba biti 11 znakova) #translates/length
As for the code, I only added config.i18n.default_locale = :hr to config/application.rb.
In the documentation in says:
Following locales are complete:
bs, da, en, en-US, es-PA, hr, is, ja, nl, sr, ur, zh-HK
Tested with other locales but is still doesn't translate the 4 errors prohibited this list from being saved: part.
Am I doing something wrong or there are translations missing in the .yml files ?
Note: I use rails 4.0.0
Update:
<%= form_for(#report) do |f| %>
<% if #report.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#report.errors.count, "error") %> prohibited this list from being saved:</h2>
<ul>
<% #report.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-container">
<div class="inline half">
<div class="field">
<%= f.label :field1 %><br>
<%= f.text_field :field1 %>
</div>
<div class="field">
<%= f.label :field2 %><br>
<%= f.text_field :field2 %>
</div>
<div class="field">
<%= f.label :field3 %><br>
<%= f.text_field :field3 %>
</div>
<div class="field">
<%= f.label :field4 %><br>
<%= f.text_field :field4 %>
</div>
</div>
<div class="inline half">
<div class="actions">
<%= f.submit "Create", class: "continue-button" %>
</div>
</div>
</div>
<% end %>
As some fields clearly translate, I think you have to look for the "errors prohibited..." message. This message should be in your activerecord.hr.yml file, and should look like
hr:
errors:
messages:
not_saved:
one: '1 error prohibited this %{resource} from being saved:'
other: '%{count} errors prohibited this %{resource} from being saved:'
(but then in your language)
Now change in your view the code
= pluralize(#report.errors.count, "error") %> prohibited this list from being saved:
into
t('errors.messages.not_saved', count: #report.errors.count, resource: Report.model_name.human)
In order to get the "Report", mentioned in Report.model_name.human translated into Hungarian, you should add something to your hr.yml file like
activerecord:
models:
report: Translation of report in Hungarian
reports: Translation of reports in Hungarian
That should give you complete flexibility...
You can try changing <%= pluralize(#report.errors.count, "error") %> prohibited this list from being saved: for <%= t( :errors, :count => #report.errors.count, :model => t("model.Report")) %> it will take the model and errors from the following places
hr:
model:
Report: "Report"
errors:
one: "%{model} can't be saved... 1 error custom message."
other: "%{model} can't be saved for %{count} custom errors"
I'm doing a simple exercise with two models. Sport and Teams, defined as
rails g scaffold sport name:integer
rails g scaffold team name:integer fans:integer sport:references
(Note: The reason I'm using scaffold is rapidly prototyping so I can learn/experiment with the parts I'm not familiar with yet)
Problem is that my "sport" (i.e. the foreign key reference) is showing like the following
So it's got that weird #<blahl blah> notation to it...
<%= form_for(#team) do |f| %>
<% if #team.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#team.errors.count, "error") %> prohibited this team from being saved:</h2>
<ul>
<% #team.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :fans %><br />
<%= f.number_field :fans %>
</div>
<div class="field">
<%= f.label :sport %><br />
<%= f.text_field :sport %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I've tried changing the one line to #team.sport.name but it results in an error undefined method 'Ice Hockey' for #<Team:0x3e7e040>... Any ideas how to properly display name from here??
You are using a text_field for referencing an existing object, a select with Sports as options would be more appropriate here.
This is where it has to be changed:
<div class="field">
<%= f.label :sport %><br />
<%= f.text_field :sport %>
</div>
To:
<div class="field">
<%= f.label :sport %><br />
<%= f.select :sport_id, options_for_select(Sport.all.map{|s|[s.name, s.id]}) %>
</div>
The f.select will generate a select box in HTML, the options will me all the sports in your DB.
Some documentation about it:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
http://guides.rubyonrails.org/form_helpers.html#select-boxes-for-dealing-with-models
A cleaner way would be to set a variable #sports in your controller and call it then in your views:
# in controller
def edit
#sports = Sport.scoped
#...
# in edit view
<div class="field">
<%= f.label :sport %><br />
<%= f.select :sport_id, options_for_select(#sports.map{ |s| [s.name, s.id] }) %>
</div>
Additionnal information: If you want to "pre-select" an option for the select, you have to pass it as the second argument of the options_for_select helper:
options_for_select(#sports.map{ |s| [s.name, s.id] }, params[:sport_id])
# this will select by default the option that matches the value of params[:sport_id]
I have a Model "Startup" and I have a other Model "Categorie". The two tables are associated.
I'd like call the data of Categoria through to form of Startup, this categories displayed with a checkbox. Inside the form Startup Form I have the categorie_id. This is the code
<%= form_for(#startup) do |f| %>
<% if #startup.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#startup.errors.count, "error") %> prohibited this startup from being saved:</h2>
<ul>
<% #startup.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.collection_select :round_id, Round.order(:name), :id, :name, include_blank: true %>
</div>
<div class="field">
<%= f.label :category %><br />
<%= f.text_field :category_id %>
</div>
<div class="field">
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: true %>
</div>
<div class="actions">
<%= f.submit %> </div> <% end %>
How to display the data of Categories within form with a checkboxs ?
Any idea.
pdt: My english is really bad.
If a Startup can have only one Category, you can do like this in your view:
<div class="field">
<%= f.label :category %><br />
<%= f.collection_select :category_id, Category.all, :id , :name %>
</div>
This will output a dropdown menu with all the categories. Make sure that the Category model has the attribute name.
As you said, a Startup belongs to one Category, so using radiobuttons (checkboxes are here for multiple relation, means you could choose multiple categories):
<div class="field">
<% Category.all.each do |category| %>
<%= f.radio_button :category_id, category.id %>
<%= f.label :category_id, category.name %>
<% end %>
</div>
You may have to add <br /> tags, and html options to make it looks better.
As MrYoshiji says, you can use:
It will display the category name on the screen, and the value sent will be the id.
You can find more details here : http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
I advise you to use the simple_form gem to generate your form. It's a very simple gem to use and customize ! look at it ;)
I am attempting to create a security model with sqlite3.
This code is inside of the _form.html.erb file inside of ../views/users/
I have a user model & a user_level model ( admin, user, etc ).
rails generate scaffold user_level desc:string
rails generate scaffold user email:string password:string user_level:references
When creating/editing a 'user', i want to be able to select a 'user_level' value from the html5 tag.
I am getting a undefined methoddesc' for nil:NilClass` error when i try to view the page.
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.text_field :password %>
</div>
<div class="field">
<%= f.label :user_level %><br />
<select id="select_user_level">
<%= #user_level.desc do |desc| %>
<option value = "1"><%= desc %></option>
<% end %>
</select>
<%= f.text_field :user_level %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= f.collection_select :user_level, User::USER_LEVEL, :to_s :humanize %>
Then in the Model you should add an Array containing all possible user levels like this:
USER_LEVEL = ["admin", "mod", "user"]
Then it should work and you dont need #user_level anymore
Given the following form:
<%= form_for(#ciudad) do |f| %>
<% if #ciudad.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#ciudad.errors.count, "error") %> prohibited this ciudad from being saved:</h2>
<ul>
<% #ciudad.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :nombre %><br />
<%= f.text_field :nombre %>
</div>
<div class="field">
<%= f.label :departamento_id %><br />
<%= f.select :departamento_id , :prompt => "Seleccione el municipio" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
How would I populate the options for the deparamento_id select tag from a database?
The simplest way to do that is to use f.collection_select instead of f.select. Assuming you have a table / model named Departamento with a field called nombre:
<%= f.collection_select :departamento_id, Departamento.all, :id, :nombre, :prompt => "Seleccione el municipio" %>
You can read more about it in the official Rails Guides here: http://guides.rubyonrails.org/form_helpers.html#option-tags-from-a-collection-of-arbitrary-objects
And in the API documentation:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select