views:

54

answers:

2

Hello... 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"

  • created styling
  • when "deleted"
  • deleted styling
  • else
  • error
  • 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.

    Thanks

    +1  A: 

    This should work:

    <%= 
      case @audit_log.description
      when "created" then "created styling"
      when "deleted" then "deleted styling"
      else "error"
      end
    %>
    
    Raphomet
    Thanks,, one issue... I want it to be HTML for the WHEN.... so when "created" <li>lots of stuff</li>.. but it errors with HTML.. how do I make Rails accept an HTML block of code when the case is satisfied?
    AnApprentice
    Maybe wrap that tag up in `<li><%= case ... %></li>`. What error are you seeing? (Personally, I prefer using `<% if %>` statements in views for readability.)
    Raphomet
    +1  A: 

    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}" %>
    
    PeterWong
    that seems like a nice clean approach. TWhat I can't wrap my head around is my database fields are ID, ACTION, TYPE, DESCRIPTION, DATA... And Data should hold a Has associated data based on the type... So I want the CASE statement to detect the TYPE, and then output the info. the info is different for every TYPE, which is why I'm using a DATA field full of meta data.... So I would need to use a CAST to detect the type, then send it to a partial like _auditlogItem.html.erb.... Perhaps in the partial, I can pass the string based on the ACTION type like "James Bond deleted the Book_id. Thoughts?
    AnApprentice
    actually I am quite confused by your database fields...What's exactly the natures are the ACTION, TYPE and DESCRIPTION? So you would determine the TYPE first, in order to fetch the DATA correctly. Then `deleted`, `created` is the ACTION or the DESCRIPTION?
    PeterWong
    Example: 1 | CREATED | PHOTO | DATA .... And Data would be a HASH (i think that's the way to go, but IDK yet) DATA would be created with an observer {photo_name: "asdadasd", photo_id: "3131"} the idea with this is to be able to output without requiring joins across multiple tables. Thoughts?
    AnApprentice
    DESCRIPTION's not here. So for each pair of TYPE and ACTION, create a partial for it naming "#{TYPE}_#{ACTION}" (ie. _photo_created.html.erb) is OK? They would be very similar or quite different? Too much uncertainty and I can't guess anymore...
    PeterWong