AnApprentice
AnApprentice

Reputation: 111040

Case statement in a view

I have an AuditLog with things like id, action, type, description...

What I'd like to do in the view is something like:

case description
  when "created"
    <li>created styling</li>
  when "deleted"
    <li>deleted styling</li>
  else
    <li>error</li>
end

Trying to learn how to do this in a view and the correct syntax, which the resources I found on Google don't specify.

Upvotes: 1

Views: 5749

Answers (3)

Mike
Mike

Reputation: 1684

actually it would be better and easier to use Jquery's addclass function

Upvotes: -2

PeterWong
PeterWong

Reputation: 16011

If your styling contains lots of tags and HTML elements, I would suggest putting them into partials:

<%= render :partial => @audit_log.description rescue nil %>

If you description is created, then it would render the file _created.html.erb in the same folder as the current view

If it is deleted, render _deleted.html.erb automatically.

If description is something else, which has no _something.html.erb file, then nothing would be displayed (without rescue nil, error will occurs)

======

If you want to render the partial in some different folder,

<%= render :partial => "some/where/#{@audit_log.description}" %>

Upvotes: 4

Raphomet
Raphomet

Reputation: 3639

This should work:

<%= 
  case @audit_log.description
  when "created" then "created styling"
  when "deleted" then "deleted styling"
  else "error"
  end
%>

Upvotes: 1

Related Questions