<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Danilo Gurovich &#187; collection_select</title>
	<atom:link href="http://gurovich.com/site/tag/collection_select/feed/" rel="self" type="application/rss+xml" />
	<link>https://gurovich.com/site</link>
	<description>Strategic eCommerce Technology and Architecture</description>
	<lastBuildDate>Mon, 25 Sep 2023 14:32:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Ruby on Rails:  Create a Select Drop Down from an Array</title>
		<link>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/</link>
		<comments>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 02:49:14 +0000</pubDate>
		<dc:creator>Danilo Gurovich</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[array object]]></category>
		<category><![CDATA[collection_select]]></category>
		<category><![CDATA[drop down]]></category>
		<category><![CDATA[Ruby LabelValue]]></category>
		<category><![CDATA[Ruby LabelValueBean.]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[select box]]></category>

		<guid isPermaLink="false">http://danilogurovich.wordpress.com/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/</guid>
		<description><![CDATA[Problem: You wish to populate a &#8220;collection_select&#8221; box with an Array. I recently had an issue where I wanted to create a select box populated from an array of continuous years. I wanted to use thecollection_select method so it would have a minimal &#8220;code footprint&#8221; and play nice with the form that I had built. [...]]]></description>
			<content:encoded><![CDATA[<div style="font-size:18px;font-style:italic;"> 			Problem: You wish to populate a &#8220;collection_select&#8221; box with an Array.</div>
<p>I recently had an issue where I wanted to create a select box populated from an array of continuous years. I wanted to use the<code>collection_select</code> method so it would have a minimal &#8220;code footprint&#8221; and play nice with the form that I had built. The application that I&#8217;m working on is in Rails2 and I didn&#8217;t want to divert heavily or make to many changes, and after some fiddling I came up with a nice solution: In the <code>environment.rb</code> file I created an Array constant to contain the year range that I wanted to use:</p>
<div style="font-size:10px;font-family:lucida,courier,monospace;">     #Constant Values<br />
YEARS_ARRAY = Array.new(89) {|i| 1920+i}</div>
<p>This code is a shortcut to create an array of values between 1920 and 2008 (88 years). It creates a new array with a size of 88, and then uses the <code>i</code> value to populate each year incrementally. I was pleased to find a method like this!</p>
<p>The form declaration contained the object that I wanted to populate with the form values using the Rails2 syntax:</p>
<div style="font-size:10px;font-family:lucida,courier,monospace;">    &lt;table class=&#8221;new&#8221;&gt;<br />
&lt;% form_for(@movie_poster) do |f| %&gt;<br />
&nbsp;&lt;tr&gt;<br />
&lt;th&gt;Title:&lt;/th&gt;<br />
&nbsp;&nbsp;&lt;td&gt;&lt;%= f.text_field :title %&gt;&lt;/td&gt;<br />
&nbsp;&lt;/tr&gt;<br />
&#8230;..<br />
&lt;% end %&gt;<br />
&lt;/table&gt;</div>
<p><span style="font-size:10px;font-style:italic;">*note the newer Rails2 syntax for the form&#8230;</span></p>
<p>I will populate my select box with an Array of Objects. I need to create my Object first to populate my Labels and Values, and I used an &#8220;old trick&#8221; from Struts by creating a Ruby <code>LabelValue</code> object that mimics Struts <code>LabelValueBean</code> utility POJO. The object has a label and value <code>accessor</code>, which works extremely well for any set of arrays since any kind of mapping scheme would consume duplicate keys and often select boxes, radio buttons, etc may need some duplication. Here is my <code>label_value.rb</code> class, located in my model directory:</p>
<div style="font-size:10px;font-family:lucida,courier,monospace;"># Creates a Label and value for select boxes and<br />
# forms without clearly delinieated# objects, such as Arrays.<br />
class LabelValue<br />
# name the accessors. Label and Value<br />
attr_accessor :label, :value<br />
end</div>
<p>Now that I have a model to work with and an Array Constant to use, I created the &#8220;helper&#8221; to use with the form tag in the <code>application_helper.rb</code> file, so I might use it throughout the application&#8217;s forms. There are two methods, one public, and one private. Note the &#8220;Select year&#8221; prompt:</p>
<div style="font-size:10px;font-family:lucida,courier,monospace;"># selection for a year value<br />
# &#8216;f&#8217; represents the passed in form value<br />
def year_select(f)<br />
f.collection_select(:year,year_lookup,:label,:value,{:prompt=&gt;&#8221;Select year&#8221;})<br />
end<br />
#  &#8212;&#8212;&#8212;&#8212; PRIVATE METHODS &#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>private</p>
<p>def year_lookup<br />
#create  an emptycollection to hold the LabelValue Objects<br />
years = Array.new()#populate the ArrayYEARS_ARRAY.each do |yr| y = LabelValue.new<br />
y.label = yr<br />
y.value = yr<br />
years.push(y)<br />
end<br />
years<br />
end</p></div>
<p>Finally, we need to call the _helper method from the form:</p>
<div style="font-size:10px;font-family:lucida,courier,monospace;">&lt;tr&gt;<br />
&lt;th&gt;Grade:&lt;/th&gt;<br />
&lt;td&gt;&lt;%= grade_select(f) %&gt;&lt;/td&gt;<br />
&lt;/tr&gt;</div>
<p>The resulting code renders a select box:</p>
<div style="font-size:10px;font-family:lucida,courier,monospace;">      &lt;select id=&#8221;movie_poster_year&#8221; name=&#8221;movie_poster[year]&#8220;&gt;<br />
&lt;option value=&#8221;"&gt;Select year&lt;/option&gt;<br />
&lt;option value=&#8221;1920&#8243;&gt;1920&lt;/option&gt;<br />
&lt;option value=&#8221;1921&#8243;&gt;1921&lt;/option&gt;<br />
&lt;option value=&#8221;1922&#8243;&gt;1922&lt;/option&gt;<br />
&lt;option value=&#8221;1923&#8243;&gt;1923&lt;/option&gt;<br />
&lt;option value=&#8221;1924&#8243;&gt;1924&lt;/option&gt;</p>
<p># continues&#8230;</p>
<p>&lt;option value=&#8221;2005&#8243;&gt;2005&lt;/option&gt;<br />
&lt;option value=&#8221;2006&#8243;&gt;2006&lt;/option&gt;<br />
&lt;option value=&#8221;2007&#8243;&gt;2007&lt;/option&gt;<br />
&lt;option value=&#8221;2007&#8243;&gt;2007&lt;/option&gt;<br />
&lt;/select&gt;</p></div>
<p>The nice thing is reusability of the <code>LabelValue</code> class, the <code>YEAR</code> Constant and the <code>collection_select</code> itelf. Everything can be accessed as it is needed, whether you want to use the Constant Array for something else, the <code>LabelValue</code> object for a different array or tuple, or the actual rendered select box in different views.</p>
<h5> 			UPDATE, 1/15:</h5>
<p><i>See the comments below for a nice alternative and discussion!</i></p>
]]></content:encoded>
			<wfw:commentRss>https://gurovich.com/site/2008/01/13/ruby-on-rails-create-a-select-drop-down-from-an-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
