Rails localization syntax - ruby-on-rails-3

I've installed Twitter Bootstrap into my project and found unknown syntax for me:
<%=t '.title', :default => model_class.model_name.human.pluralize %>
<%= link_to t('.new', :default => t("helpers.links.new")),
new_article_path,
:class => 'btn btn-primary' %>
I can't understand the meaning of '.title', '.new' and 'helpers.links.new'. How do these constructions interact with the locale dictionary?
Also I've never met the construction :default => in t method, where I can read about it?

t is a helper method supplied by I18n internationalization mechanism of rails, and is a shortcut for I18n.translate method.
The locale file which I18n reads from is set by default to Rails.root/config/locales/en.yml assuming en is your default locale.
The first argument is the key which I18n will look for in your locale file.
The statement t('.new', :default => t("helpers.links.new")) means that I18n will look for the construct
en:
new: "new string"
in your locale file.
:default is the string which will be returned in case the first key was not found.
:default => t("helpers.links.new") just means that I18n will look for the following construct in en.yml:
en:
helpers:
links:
new: "new string"
and return it in case the first one was absent.
You can find here the full documentation of I18n translate method.

Related

Why is the word "notifications" causing i18n to break in rails?

I'm having a really weird issue with i18n on a current rails project. I get translation missing messages whenever I attempt to use the key notifications. For example:
#en-US.yml
en-US:
notifications:
index:
title: 'notification page'
#/notifications/index.html.erb
<%= content_for :title, t('.title') -%>
This will fail and tell me the translation is missing
#en-US.yml
en-US:
notifications:
index:
title: 'notification page'
#/notifications/index.html.erb
<%= content_for :title, t('notifications.index.title') -%>
This will also fail.
#en-US.yml
en-US:
notification:
index:
title: 'notification page'
#/notifications/index.html.erb
<%= content_for :title, t('notification.index.title') -%>
This oddly, will work, removing the s will allow it to find the translation.
#en-US.yml
en-US:
notifications1:
index:
title: 'notification page'
#/notifications/index.html.erb
<%= content_for :title, t('notifications1.index.title') -%>
This also will work, adding a 1 to the end of notifications works.
It appears that rails does not like the word notifications. This is a problem because I don't want to have to rename the entire model for this, and also I want to use the i18n.t view shortcuts for consistency. Is notifications a reserved word? Is there any reason why it is failing to find it?
I'm not sure why it's failing in your case but I just had a go myself and it's not a reserved word, my locale file:
en:
notifications:
foo: bar
The result:
1.8.7 :001 > I18n.t('notifications.foo')
=> "bar"
This test was done with Rails 3.1.5 and Ruby 1.8.7.
In your case you got the following?
1.8.7 :001 > I18n.t('notifications')
=> "translation missing: en.notifications"
Although not an exact answer, I have found that there are a few words in I18n that cannot be used as keys. I am assuming the I18n code uses them elsewhere.
Others include "no", "yes", "on", etc.
Just using a synonym or do something like "notifications_" to make it different.

url_for adding controller and action to querystring in rails 3.2

I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.

Render #object and locals vs render :partial

I want to pass a local variable that contains the origin to come on a specific page, this variable contains just a symbol with the value.
When I use this code it works perfect, the origin variable is accessible in the partial :
render :partial => "products", :collection => #products, :locals => {:origin => :gallery}
But when I use this code, the origin is not set and not accessible in the partial :
render #products, :locals => {:origin => :gallery}
What is the difference here? Is the second line of code not render the partial like the first line?
<%= render #products %>
Is indeed the shorthand syntax for rendering a partial. But with the shorthand syntax, Rails will ignore the ":locals" variable. There's more on this in the Rails Guides.
So if you want to pass extra options to the render, you have to specify ":partial => ...". If you want to know why this happens, you can take a look at the Rails source.
There's a good explanation here: Rails: confused about syntax for passing locals to partials
The short version is that you can just omit :locals in the second example:
render #products, :origin => :gallery

HAML form_tag problems with dropdown menue

%h2 Your "followers":
- form_tag twitter_path do |f|
= f.select{:name => "dropdown"}
- for follower in #followers
%option{:value => follower['id']}= h follower['name']
= f.submit_tag "Who leaves comments?"
How do you properly format this HAML? Its returning a Syntax Error.
You're using HAML syntaxe inside Ruby code here:
= f.select{:name => "dropdown"}
The brace { is interpreted as the begining of a Ruby block (like in array.map { ... }), because everything after the = or - prefix in HAML is evaluated as Ruby code.
Also, you're using the form_for syntax while using the form_tag method (see this question).
The form_tag method doesn't provide a form object f.
You should use the select_tag method from FormTagHelper instead, along with a FormOptionsHelper method:
- form_tag twitter_path do
= select_tag "dropdown", options_from_collection_for_select(#followers, "id", "name")
= submit_tag "Who leaves comments?"

Is there a new syntax for `url_for` in rails 3?

In a plugin helper, I have:
include Rails.application.routes.url_helpers
url_for(:only_path => true, :controller => 'people', :action => 'new')
Note that uses the new include syntax, that part works ok. But I get an error:
undefined local variable or method `controller' for #<ActionView::Helpers::InstanceTag:0x311ddf4>
Is there a new way to specify this, like maybe 'controller#action' ? what's the key?
url_for should work as usual, see http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-url_for
I checked it on my console:
ruby-1.9.2-head > include Rails.application.routes.url_helpers
=> Object
ruby-1.9.2-head > url_for(:only_path => true, :controller => 'admin/providers', :action
=> 'new')
=> "/admin/providers/new"
Maybe the error doesn't occur in the url_for because your error messages says ActionView::Helpers::InstanceTag this sounds like you're using some kind of Tag like link_to etc. Did you think about this?
Best regards
Simon