SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    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

  2. #2
    l º 0 º l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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-tutorialsblogtwitterlast.fm

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Nice with the helper example

  4. #4
    SitePoint Enthusiast TomK32's Avatar
    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.
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •