cleaner Ruby in a rails view

D

dblack

Hi --

I think that is the best answer so far....but that seems to go against
everything that makes ruby so great.

That doesn't say much for all the other answers :)
Something just doesn't feel right about creating an object just to
say that it's empty. I guess I should ask this on the rails list,
but I like this list much better :) , Is is possible to add methods
to the nil class in only the views? If so, that would be awsome!

I suppose you could engineer a way to do that, but it sounds awfully
fragile, and like an awful lot of trouble to go to. I'd rather
normalize the data to an array (or whatever) and then just let each
array take care of itself.


David

--
David A. Black ([email protected])
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
C

Charlie Bowman

--=-c2qOSI+FyezlnPoTq3Rn
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

I guess I am being a little picky. ruby and rails both are the best at
what they do and it's hard to watch it fall short ( in my very picky
eyes ) in this one area. I am going to work on adding a plugin to rails
that will fix the nil problem. At least then I won't have to call a
helper method, rescue nil, or create an empty object just to display a
view. This has been a great discussion! It's too bad that it wasn't on
the rails list...

I suppose you could engineer a way to do that, but it sounds awfully
fragile, and like an awful lot of trouble to go to. I'd rather
normalize the data to an array (or whatever) and then just let each
array take care of itself.


David


Charlie Bowman
http://www.recentrambles.com

--=-c2qOSI+FyezlnPoTq3Rn--
 
M

Matthew Desmarais

Charlie said:
I guess I am being a little picky. ruby and rails both are the best at
what they do and it's hard to watch it fall short ( in my very picky
eyes ) in this one area. I am going to work on adding a plugin to rails
that will fix the nil problem. At least then I won't have to call a
helper method, rescue nil, or create an empty object just to display a
view. This has been a great discussion! It's too bad that it wasn't on
the rails list...





Charlie Bowman
http://www.recentrambles.com
You know, I don't much monitor the rails list but I think that maybe
this is something that you would want to run by those folks before you
make any big design decisions. The folks over there really do spend a
lot of time on rails-specific issues and you'll probably get more
discussion over there if not better. I'd be curious to hear what they'd
say about this myself since I've been where (I suspect) you are before.

Regards,
Matthew
 
S

ssmoot

I think the "and-call" operator discussed recently would be a very very
very nice addition, especially since in Rails Views (or any
presentation layer on most any framework in most languages) has the
ever-so-common "obj ? obj.value : nil" mini-pattern.

Of course it doesn't really apply in this specific case since there's
an empty array available, but:

item.cart_item_option_values &? each do { |ov| blah }

Would be nice.
 
S

Seth Thomas Rasmussen

Hi all,

Interesting discussion... I'll just add that the following:
<% item.cart_item_option_values.each do |ov| %>

Can be:

<%= render :partial => 'foo', :collection =>
item.cart_item_option_values %>

_foo.rhtml would contain the markup used to render each item in the
collection. Sometimes I also like to do this, however:

<%= render :partial => 'foos', :eek:bject => some_collection %>

And then in _foos.rhtml have logic like:

<% if foos -%>
<ul> <!-- or whatever parent container is appropriate -->
<%= render :partial => 'foo', :collection => foos %>
</ul>
<% end -%>

In the latter example, you also might want to forego the collection
rendering capability and use ERb tags to do an each() block, simple
because too many levels of partials becomes cumbersome.

Either way, it's generally more pleasing (if not outright useful, say,
for less savvy designers) to remove more complicated logic from the
'root level' view templates. The reason being, I think, because once
you get to a certain point, you're logic is solid, but you're markup
structure and flow might demand tweaking.
 
S

Seth Thomas Rasmussen

Forgot to add that with the second example, you'd probably want an <%
else -%> clause to output something useful in that event, but you're a
clever bunch. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,500
Members
48,187
Latest member
ashish_758

Latest Threads

Top