Posts Tagged ‘HAML’

I’m working on a legacy app that combines the use of HAML and erb markup templates for Ruby on Rails.

ROR deals with both templating markups seamlessly when used in the same application, but I noticed an issue when using a textarea element in a form on the page.

The problem:
I have a free text field that loads up an address. Every time the app would load the data from the database and display it in the textarea field, the resulting text would have extra spaces on second or third lines. Even if you deleted the spaces and saved the address, they would reappear next time I edited the field.

No styling, and no gsub action would fix the problem. Ultimately, it was caused because the template that rendered the form was an ERB template. In all other cases, the ERB templates rendered just fine, but when it came to textarea elements, it adds the white space. Normally, this isn’t a problem, but there seems to be a conflict when using HAML and ERB in the same app.

The solution:
It’s simple, convert the ERB template to HAML.

That’s it.

-Chris

fields_for and HAML

Sunday, March 8th, 2009

Found this little quirk in the templating markup language HAML and Ruby on Rails.

Where you can use the fields_for with the same block variable in an erb template file and then continue processing fields for the top level form like so:

1
2
3
4
5
6
7
<% form_for :user, @user do |f| %>
  Process fields for the user
  <% fields_for "address", @screening_address do |f| -%>
     Process fields for the address
  <% end -%>
  Process more fields for the user
<% end -%>

It just won’t work in HAML. You have to explicitly rename the block variable for the fields_for block:

1
2
3
4
5
- form_for :user, @user do |f|
  Process fields for the user
  - fields_for "address", @screening_address do |add| 
      Process fields for the address
  Process more fields for the user

Apparently, the block level variables when executed in a HAML template seem to stick around even when you don’t expect it to.

It tripped me up for a few minutes, so I thought I’d put it up.

-Chris

Event Clipboard