Archive for November, 2007

Rails Notes

12 Nov

I’m reading through Agile Web Development With Rails tonight and have come across a couple of interesting tidbits that may be good to know later.

When writing code in .rhtml files

<% 3.times do %>
  Test<br />
<% end %>

will result in the following html

Test<br />

Test<br />

Test<br />

to prevent this add a minus (-) before the close of the code sections to indicate that the last line break should be ignored.

<% 3.times do -%>
  Test<br />
<% end -%>

Also there is a helper method “h” that is really nice. It will parse any string into the markup required to cause the string to be rendered as it should. This eases site design and helps to protect from some hacks. So

<% h(“3 < 4”) %>

is translated to

3 &lt; 3

as desired.

Also something that is worth noting is that the views have access to all instance variables in the controller that calls them, even private variables.