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"
Related
My form::
<h1>New Post</h1>
<%= form_for (#post), url: posts_path do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :Title%><br>
<%= f.text_field :Title%>
</p>
<p>
<%= f.label :Text%><br>
<%= f.text_area :Text%>
</p>
<p>
<%= f.submit :Save %>
</p>
<% end %>
:: context ::
It is not showing the error messages. please point out the where i am wrong. I want to add basic validations. i have gone through some tutorials but still stuck into this.. please help me out.
In your Post model,Just add these line
validates_presence_of :title,text #doesn't allow the blank values
If you are also looking for validation of unique values for title,add this line also.
validates_uniqueness_of :title #doesn't allow duplicate values
Also Follow these Guides(Rails 4),if you are looking for more validations.
validates :title, uniqueness: {message: "must be uniq"}
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}"...
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
I created 2 tables called products and brands and created a join table called brands_products via migration.
In each of the models I wrote the corresponding has_and_belongs_to_many setting.
In a form if have the following code:
<%= form_for(#product) do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.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 :brand %><br />
<%= f.text_field (what to write?) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I don't know how to add a brand to a product intuitively, like the way Rails usually works...any thoughts?
You may want to look at accepts_nested_attributes_for. You'll also want to look at fields_for. Without seeing more of your data model, it's hard to give a detailed answer.
Generally, it would look something like:
<%- f.fields_for :brands do |m| -%>
<%= m.text_field :name %>
<%- end -%>
i am following this simple tutorial and i followed all the steps... but the browser simply doesn't show me anything i put inside the fields_for tag.
<%= form_for :activity, :url => activities_path do |f| %>
<% if #activity.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#activity.errors.count, "error") %> prohibited this activity from being saved:</h2>
<ul>
<% #activity.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Foto" %><br />
<%= f.text_field :photo %>
</div>
<div class="field">
<%= f.label "Foto per la home" %><br />
<%= f.text_field :photoHome %>
</div>
<% for info in #activity.infos %>
This is rendered<br>
<% fields_for "activity[info_attributes][]", info do |info_form| %>
This is not rendered<br>
<p>
Titolo: <%= info_form.text_field :title %>
</p>
<p>
Descrizione: <%= info_form.text_field :description %>
</p>
<% end %>
<% end %>
<p><%= submit_tag "Create activity" %></p>
<% end %>
The above code result is this:
What am i missing?
Rails is sometimes (well ok, often) a bit confusing in which helpers want a <% vs which helpers want <%=. Try
<%= fields_for "activity[info_attributes][]", info do |info_form| %>
The tutorial you're following doesn't, but that's from 2007, and in Rails 3 this was changed. Current documentation on fields_for is here.