SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: views formatting
-
Mar 31, 2008, 14:27 #1
- Join Date
- Jul 2006
- Posts
- 74
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
views formatting
Hello,
My messages model got a "status" field. What i would like to do is wrap <strong> around the link if message.status = new. How should such a task be sovled in RoR?
VIEW:
Code Ruby:<% for message in @messages %> <li> <%= link_to message.subject, {:action => 'show',:id => message.id} %> </li> <% end %>
Best regards.
Asbjørn Morell
-
Mar 31, 2008, 16:38 #2
The easiest and straightforward approach is just simple conditionals:
Code Ruby:<% for message in @messages %> <li> <%= "<strong>" if message.status == 'new' %> <%= link_to message.subject, {:action => 'show',:id => message.id} %> <%= "</strong>" if message.status == 'new' %> </li> <% end %>
It's a bit heavy, though, if you're using this all over the place. At that point, you might want to move the whole process into a helper (/app/helpers). Just keeps it a bit simpler:
Code Ruby:<% for message in @messages %> <li> <%= message_link(message) %> </li> <% end %>
Code Ruby:#in a helper file def message_link(message) if message.status == 'new' "<strong>#{link_to message.subject, {:action => 'show',:id => message.id}}</strong>" else link_to message.subject, {:action => 'show',:id => message.id} end end
...something like that. There's about a billion ways to tackle this, simplify it, refactor it, etc..
Zach Holman
good-tutorials — blog — twitter — last.fm
-
Apr 1, 2008, 07:17 #3
- Join Date
- Jul 2006
- Posts
- 74
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks. Nice with the helper example
-
Apr 5, 2008, 01:29 #4
- Join Date
- Mar 2008
- Location
- Berchtesgaden, Germany
- Posts
- 32
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just for the case you are using the same method in your controller and your views you can define the method in your controller and then use helper_method :my_method to propagate it to the view.
Bookmarks