views:

52

answers:

5

Ok, i'm working on views for my little project. Every day I make things like this:

<li <%= 'class="'+item.status.caption+'"' if %w{ sold, absent }.include?(item.status.caption) %> >

Recently, I found a proper String method in Ruby API, called quote. I wrote this:

<li <%= 'class='+item.status.caption.quote if %w{ sold, absent }.include?(item.status.caption) %> >

but application dropped an error, saying it does not recognize the method. So, is there a proper method to do such usual quotation (or, even, 'somethination' with a custom symbol/string :P )? Anything like 'surround_with' or something? Didn't found a clue in Rail API.

A: 

To use quotes, I'd do something like this:

"This is '#{string_inside_quotes}'."

Or

"This is \"#{string_inside_quotes}\"."

though it's not quite clear to me what you mean in your original example.

Peter
I'm trying to get rid of those manually placed quotes
gmile
A: 

Why not just

<li class="<%= item.status.caption if %w{ sold, absent }.include?(item.status.caption) %>">

as an empty class attribute is like no class attribute?

If you don't want to escape your quotes there is also another way to quote in ruby:

<li <%= %q(class="#{item.status.caption}") if %w{ sold, absent }.include?(item.status.caption) %>>
Peter Krenn
Yeah, I've done that in early stages. But don't much like those empty class= attributes, left on page.
gmile
I updated my answer.
Peter Krenn
What's that %q stands for?
gmile
It's a way to quote strings in Ruby. http://en.wikibooks.org/wiki/Ruby_Programming/Alternate_quotes
Peter Krenn
+1  A: 

You could also create a pretty trivial helper, for example:

  def quoted_class(val)
    "class='#{val}'"
  end

Then you'd use it in your example like so:

<li <%= quoted_class(item.status.caption) if %w{s,abs}.include?(item.status.caption) %>>
JRL
Hmm, I believe that will do. Thanks :)
gmile
+1  A: 

Or you could use content_tag? You don't say what you are displaying within the li, but you could do something like:

<%= content_tag :li, "your list content", ({:class => item.status.caption} if %w{ sold absent }.include?(item.status.caption)) %>

By the way, I don't think you want the comma in your %w string, unless you really want to match against "sold," (with a comma) or "absent".

NeilS
Yeah, already deleted that come, thanks ) And surely thanks for content_tag, that's probably the most convenient way
gmile
+2  A: 

That's way too much logic inside a view, and you're not using the helpers Rails gives you. Make a method in your foo_helper.rb file in app/helpers (where 'foo' is your controller name):

def display_item(item)
  case item.status.caption
    when /sold/ then class = 'sold'
    when /absent/ then class = 'absent'
    else class = nil  # You could add more cases here as needed
  end
  content_tag :li, item.name, :class => class  # Or whatever you want shown
end

Then, in your view, you can call <%= display_item(item) %> instead of all that embedded if gibberish.

(Oh, and this wasn't your question, but as an additional exercise, also think about how to reduce that item.status.caption chaining, either with methods like item.sold? and item.absent? or by turning status into something you can test directly. Google on "Law of Demeter" to find out why this is a good idea.)

SFEley
Hmm, thanx. I'm convinced that for now my views are pretty dirty, and certainty was going to do something about that. Thanks for additional pushing :)
gmile