[Rails] Re: Problem with select and each_with_index

Mark Reginald James mrj at bigpond.net.au
Wed Mar 1 09:18:34 GMT 2006


Chris T wrote:

> Thinking about this again and re-reading your email, think I'm beginning 
> to see the light. If I'm understanding it correctly, the fact that
> <%= select ('type', 'typecategory', Typecategory.each {|type| type} )%>
> produce a select box feating Typecategory elements is nothing to do with 
> each runnning through the elements, it's just acting on Typecategory. In 
> fact whatevery enumerations are done on typecategory will not change 
> what comes out (cue dim light-bulb being switched on).

Yes, each_with_index does its work through the side-effects of evaluating
its block for each array element. It evaluates simply to the array being
operated on.   But here you have inline code that you want to return a
value that becomes the option parameter to the select.

You can still use each_with_index, but you must then use more than
one line of code:

<% options = []
    Typecategory.each_with_index {|item, index| options << [item, index]} %>
<%= select :type, :typecategory, options %>


-- 
We develop, watch us RoR, in numbers too big to ignore.



More information about the Rails mailing list