I was wondering what would be the best solution for customising the rails helper: time_ago_in_words, so that I could do things like:
<span class="one">4</span>
<span class="two">hours</span>
<span class="three">ago</span>
or what the layout looks like by default?
Thanks
I would just create a helper that splits the result of time_ago_in_words on spaces and returns the array of words. Then you can format them however you want.
Instead of the method time_ago_in_words, you can use distance_of_time_in_words, which accepts locale in the option array: create a new locale where you can define "hours" with "hours", etc... and give it to distance_of_time_in_words. It should work, but I'm not sure how will be handled the HTML tags.
Related
I have a following translation:
welcome: Hello there, %{general_kenobi}
Now I know, that I can put HTML tags in the translation, like <em> or so. But what if I want this general_kenobi to have my custom style? How do I implement it? I've tried to google it, but didn't find anythin useful (or maybe I'm just bad at googling). Thank in advance!
If you want to add something fancy like this to your locale you must add the _html extension to the key.
From the docs
Keys with a '_html' suffix and keys named 'html' are marked as HTML
safe. When you use them in views the HTML will not be escaped.
en:
welcome_html: "<b>Bolded text</b>"
then just the regular stuff in your views
<%= t('welcome_html')
I have a normal tag:
...
I need it to look like this:
<a href="prefetchThisPage.html" data-prefetch> ... </a>
Why? jQuery Mobile feature: http://jquerymobile.com/demos/1.1.1/docs/pages/page-cache.html
The expected way below does not add the attribute, probably because Tritium ignores empty attributes.
attribute("data-prefetch")
Any ideas?
Great Question!
The best way to do this, is to set the value of the variable to the NAME of the variable.
I would do it like this:
$variable_name = "data-prefetch"
$("./a") {
attribute($variable_name, $variable_name)
}
This is an HTML5 convention and should work for the purposes you illustrated!
Tritium currently has a short circuit that converts attribute assignments like the following declaration:
attribute("data-prefetch", "")
Into the equivalent of removing the attribute.
I strongly recommend you file this issue with Moovweb's help desk so that they add support for adding attributes with no values.
I have some view code:
<span data-something="[<%= t('.asd') %>, <%= t('.dsf') %>]></span>
I use this code to get some dynamic strings translated into the view. My YAML is something like:
en:
feature:
asd: "Asdddd"
dsf: "adasdsadasda"
Is there a way I can use to dynamically get all the "features" from the YAML by locale and put it in the data-attribute?
This is pretty simple to do:
I18n.translate('feature').values.join(', ')
You'll end up with a string "Asdddd, adasdsadasda".
I think it's as easy as t('feature') to get the hash, you might want to just have the values so could you try t('feature').keys?
I have this:
= link_to user_path(f.object.user) do
%span.hourly-rate>= f.object.user.hourly_rate.to_currency
\/hour
Which renders as:
<a href="/users/44"><span class='hourly-rate'>$16</span>/hour
</a>
The close tag is on a separate line. This results in the link looking funny when there is underlining (eg. on hover)
The solution is to have the markup look like:
<span class='hourly-rate'>$16</span>/hour
But I haven't found a way of doing that in Haml. I'd prefer to keep using the block form of link_to if possible (I imagine not using the block would result in even messier code, but at this point I'm open to anything).
Tangent: I imagine this would be fixed if I was using the :ugly Haml option. I have Haml::Template.options[:ugly] = true in my config/environments/development.rb, and I have haml-rails in my Gemfile (I know it doesn't include :ugly, but it's worth mentioning), and I have an initializer with;
# config/initializers/haml.rb
Haml::Template.options[:format] = :html5
Haml::Template.options[:ugly] = true
... but the code still isn't ugly :( Any ideas why not?
If you can live with the content wrapped inside another div, you can use this:
= link_to user_path(f.object.user) do
%div<>
%span.hourly-rate>= f.object.user.hourly_rate.to_currency
\/hour
The span was being used by javascript, to get its value (in this case, the user's hourly rate) - it wasn't being used for CSS at all.
Thus, I ended up removing the span and adding the user's hourly rate as a data- attribute of a separate field. So the final markup for the link was
= link_to f.object.user.hourly_rate.to_currency + "/hour", user_path(f.object.user)
I'm working on a Rails application and using HAML for the views. I would like to use the "sliding doors" technique to create pretty buttons, which means I need to get HAML to generate something similar to the following HTML:
<span class="button">Button text</span>
I haven't been able to figure out how to do that from the HAML reference or Google. Could you help please? Many thanks in advance.
EDIT: To clarify, I need to use the link_to helper because I'm linking to a resource. When I try to pass a block into the method, I get an error: undefined method 'stringify keys'
Try
= link_to invitation_path(invitation), :method=>:put, :class=>"button" do
%span(class="button")
Accept
This should be as simple as:
%a(href="#" class="button")
%span(class="button") Button text
Or if you especially want it on one line without any whitespace you can do:
%a(href="#" class="button")<
%span(class="button") Button text