String interpolation trouble with Haml and Spree - ruby-on-rails

I'm trying to convert my Spree store to Haml, and I'm facing some trouble with some parts of the backend.
For instance, I take the following Haml code:
- #orders.each do |order|
%tr{:class => "state-#{order.state.downcase} #{cycle('odd', 'even')}"}
%tr{:class => "state-#{order.state.downcase}"}
And here's the output HTML (simplified to a single iteration):
<tr class="state-#order.state.downcase"></tr>
<tr></tr>
Can someone help me understand why the string interpolation is wrong in both cases? I have been looking at this for hours...

Your HAML syntax looks fine, but it's possible cycle is causing trouble and failing silently. Cycle is a RAILs method within ActionView::Helpers::TextHelper, so perhaps your code no longer is including that library now that you've dropped Spree.
If this is the source of the problem, you could either include the ActionView Helpers or rewrite your code without cycle with something like the following:
%table
%tbody
- #orders.each_with_index do |order,index|
%tr{:class => "state-#{order.state.downcase} #{%w(even odd)[index%2]}"}
%td{:class => "state-#{order.state.downcase}"}
#{order.state}
PS - I'm assuming the second tr in your sample code is supposed to be a td and that you're just providing a fragment of the table.

I have just revisited this and came across the fact that Deface (used by Spree) is not compatible with Haml.
So either one uses ERB and lives with it, or completely disables Deface and rewrite all 'defaced' views.
P.S.: In order to disable Deface, I added config.deface.enabled = false to development.rb, production.rb and test.rb inside config/environments/.

Related

html_safe on whole html code block in rails

i can't seem to find a way to escape whole html block, if it is even possible.
Here is what i am trying to do:
%table
%tr
%th{rowspan: "2"}= t("public.home.graphic_title_payment").html_safe
%th.datefield{rowspan: "2"}= t("public.home.graphic_title_dates").html_safe
%th{colspan: "3"}= t("public.home.graphic_title_pay_group").html_safe
%th{rowspan: "2"}= t("public.home.graphic_title_unpaid").html_safe
Altho i dont like purring .html_safe at the end of every string. Is there a way i could maybe put some kind of indicator that all %tr needs to be html_safe??
Thanks :)
Rails escapes ERB output by default. So the best thing would probably be to create a new helper method to run the I18n translation and mark it as safe. So perhaps:
# app/helpers/application_helper.rb
def st(translation)
t(translation).html_safe
end
Then:
%th{rowspan: "2"}= st("public.home.graphic_title_payment")

Rails automatically escpaping HTML - how to stop it?

I'm working on upgrading an old Rails app (1.1.6) to Rails 3. Obviously, a lot has changed. One thing appears to be that Rails automatically escapes content dropped into the view. However, I have a situation where I have a helper generating IMG tags for me, and Rails is automatically escaping the resulting content.
<%= random_image('public/images/headers') %>
This results in escaped content, much like one would expect had I done this (in 1.1.6)
<%= h random_image('public/images/headers') %>
Is there a way to tell it to not escape?
<%= raw random_image('public/images/headers') %>
.html_safe
It may need to be inside the helper
There are there ways in which this can be achieved in rails 3 application
html_safe
raw
h
raw and h can only be used in controller and views these methods are defined in helpers.
html_safe can be used anywhere in a rails application i.e., can be used in models, helpers, controller etc.
For more information please read http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/

ReCaptcha problems in Ruby on Rails

I have followed the directions posted on ReCaptcha's site about integrating Recaptcha into my application. However when I go to view it on my localhost I just get the code for get_captcha() instead of the widget. I have made the public keys available in my environment.rb file, added the recaptcha::apphelper to my application controller, and chained my vaildation, I have also included the recaptcha::viewhelper to my application helper. Am I missing a step in adding it, or is there another resource that would make this work better?
In the view you must use an ERB output block (<%=) to make this output the captcha:
<%= raw(get_captcha(:options => {:theme => 'white', :tabindex => 10) %>
Anything not in this (or it's brother, the ERB evaluation block (<%)) will be treated as plain text.

How to use Rails I18n.t to translate an ActiveRecord attribute?

Trying to use my active record translations in config/locales/de.yml also in my views. I thought I am clever using this:
de:
activerecord:
attributes:
user:
login: "Benutzerkennung"
comment: "Bemerkungen"
And in my view this:
<%= label_tag :login, t('activerecord.attributes.user.login') %>
But instead of the translation value ("Benutzerkennung") I am getting the infamous
"translation missing: de, activerecord, attributes, user, login"
Has anybody got this working (not using the label translation plugin (I am wary of potential side effects), or User.humanize_attribute_name)? What am I missing? (it does work when I use "activerecord1" or something else than activerecord, so my setup seems to be fine)
Thanks!
Ok, my bad, it does work just fine. I fell in the YML formatting trap :(
To help you debug along the way, use "script/console" and the following statements:
- I18n.locale --> should output the locale you want to examine
- I18n.t('activerecord.attributes') --> should give you all the key/value pairs for your translation, if not, you made a formatting error in your YML file or it could not be found
And btw - the plugin works quite well http://github.com/iain/i18n_label/
if you don't like the result of ".human_name" (which the plugin uses), just fall back to I18n.t('your key')
Another method:
<%= label_tag :login, User.human_attribute_name(:login) %>
You should upgrade Rails gem to v2.3.11 (I tried to use 2.3.9, but now days it is not available so I suggest you 2.3.11)!
gem install -v=2.3.11 rails
You can find this issues documented here: Rails 2.3.9 Release notes

Issue with Haml files

Hi i converting rails views from erb to Haml .The issue i faced is when use the form_for the haml throws the UNEXPECTED $end error. I am sure i did the space indentation well with in form_for .......Even if i use "each do" loop is says the same error. if i revert the haml to erb it works fine.
Rails version i used : 2.3.2 & i installed haml gem 2.2.19 as well as haml plugin also.
my controller code :
def new
#user = User.new
end
My View code :
.contentContainer.signup
- form_for(#user) do |f|
Make sure your haml indentation is perfect.
.contentContainer.signup
- form_for(#user) do |f|
= f.text_field :name
Are you including - end in your templates? Haml takes care of ends for you, so if you add your own, it won't work.
Can you paste your entire template (in a code block, so it's formatted properly)?
There's also a good command-line tool to make transition easier: html2haml. It doesn't always produce the prettiest haml, but it certainly works.
Hey, there's even a web-based form for this: http://html2haml.heroku.com/
Generally, be sure your indentation is perfect. haml is very particular about indentation. If you use a decent editor (such as textmate or vim) this is an easy task.
If the last line in file is indented there has to be an addidtional, empty line.

Resources