How to use the HAML t() helper in RoR - ruby-on-rails

I'm developing an application with Rails 4 and I want to use the helper t() with HAML, but I can not
Example usage:
=model_name = #article.class.model_name.human
%h1 Create t(:model_name)
My first line shows me the correct article, but when I use t() it shows me t(:model_name). Thanks.

hope in translational file you have en.yml
en:
article: MyArticle
Then
%h2= t 'article'
if you want dynamically
%h2= t '#{model_name}'

Related

In my haml file, I want to include a link within a string that is coming from a yml file

Here's my situation:
I have this in my haml file, ...it's a start:
.image-credits
%div
%h2
= t(:image_credits)
%ul
%li
%p
= t(:photographer_1)
And then in my yml file I have these strings defined:
image_credits : "Image Credits"
photographer_1 : "\"Miss Kitty\" by Mike Henderson"
photographer_1_link : "https://www.flickr.com/photos/mikehenderson/3750940230"
Ideally, I would like it if it was rendered like this:
"Miss Kitty" by Mike Henderson
Any suggestions on how to do that? I've been googling around but I don't think I'm searching for the right thing.
You need to do two things:
Your photographer_1 string needs to be rendered as HTML, which you signal to Rails by appending _html to the key (so it's now photographer_1_html (http://guides.rubyonrails.org/i18n.html#using-safe-html-translations)
You need to pass a parameter to this key, which would be the link you want inserted. Because links are the same in all locales, the link can be in your HAML. The parameters are passed as options in the t() method.
So you end up with:
# in en.yml
photographer_1_html : "<a href='%{link}'>\"Miss Kitty\" by Mike Henderson</a>"
# in yourview.html.haml
= t(:photographer_1_html,
link:"https://www.flickr.com/photos/mikehenderson/3750940230")

how do put %span inside rails link_to in haml

font awesome inside link_to helper with haml
how should I do?
i have this code
.link_header
{link_to_ledger(current_ledger) span.icon-flag }
help please
Just pass span as block to link_to helper method something like following:
.link_header
= link_to_ledger(current_ledger) do
span.icon-flag
There are two options for link in rails
As simple
= link_to 'my link text', controllers_path
As you want to do
.link_header
= link_to_ledger(current_ledger) do
%span.icon-flag
-# you can also do what ever you want here even table/images can also be added

How Do I Use Localization (i18n) with Haml in Middleman

The examples on Middleman's web page are in ERB, but I like to use HAML.
http://middlemanapp.com/advanced/localization/
This is their example localization YAML file:
---
es:
hello: "Hola"
And this is how they use ERB to access it.
<%= I18n.t(:hello) %> World
But I prefer HAML to ERB. How does the above line translate into HAML?
This would simply be :
= I18n.t(:hello)
World
You might want to put the greeting in your localization file though :
es:
hello: "Hola %{name}"
and call it from your HAML like this
= I18n.t(:hello, :name => 'World')

How to create a website form field in Rails 3?

I have a form where I'd like to be able to input a web address, like "google.com", into a form field, and be able to click on this link in the show view of the submitted form.
How can I accomplish this?
show.html.erb
<p>
<strong>Website:</strong>
<%= link_to #video.website, #video.website %>
</p>
Scaffolding will handle it like Jason Swett said. If you are looking to put it in a link in show just do something like this:
<%=link_to #link.name, "http://"+#link.url%>
If that doesn't work you could always do:
<%=#link.name%>
Scaffolding will handle this for you. I recommend going through the Rails Guides, especially this one: http://guides.rubyonrails.org/generators.html
And more specifically, you can generate a model with an attribute called url like this:
rails generate scaffold Thing url:string
And then run your migrations:
rake db:migrate
You should end up with some views in app/views/thing/ and a controller at app/controllers/thing_controller.rb. That should put you on your way.
If you have a model that has an attribute like web_address, you can do:
= text_field_tag, "web_address", ""
(using HAML syntax and tags for form_tag)
And then in your show you can do:
= link_to model.web_address, model.web_address
Which will make a link like: http://www.google.ca

How to define localization for names starting with a dot in Rail3?

Rails3 + HAML generated templated contains localization. For example to translate 'show'
%td.action= link_to t('show'), tidbit
I just have to add follwing to en.yml
en:
show: "Show"
but when attributes to be translated starts with a dot as in the case of '.newtidbit', this does not work.
%p= link_to t('.new_tidbit'), new_tidbit_path
If you prefix your translate keys with a period it's going to do a "lazy lookup". So you'll have to define the translation under your controller and view name such as:
en:
tidbits:
index:
new_tidbit: New Tidbit
(I'm guessing on the name of your controller and view file of course but you get the idea)

Resources