<?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>Rory Hansen &#187; ajax</title>
	<atom:link href="http://www.roryhansen.ca/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.roryhansen.ca</link>
	<description>Affiliate marketing, Internet marketing, web development, and small business ideas.</description>
	<lastBuildDate>Tue, 06 Oct 2009 06:55:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Related Drop-down Lists with Ruby and Ajax</title>
		<link>http://www.roryhansen.ca/2005/06/21/related-drop-down-lists-with-ruby-and-ajax/</link>
		<comments>http://www.roryhansen.ca/2005/06/21/related-drop-down-lists-with-ruby-and-ajax/#comments</comments>
		<pubDate>Tue, 21 Jun 2005 06:27:29 +0000</pubDate>
		<dc:creator>Rory</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.roryhansen.ca/?p=9</guid>
		<description><![CDATA[Note: I have been meaning to re-write this post to make better use of Ruby on Rails helpers, etc. When I first wrote this I was extremely new to Rails and my unfamiliarity with the framework showsUntil I have the free time to re-write this post, please bear with what is currently here and leave [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Note:</strong> I have been meaning to re-write this post to make better use of Ruby on Rails helpers, etc. When I first wrote this I was extremely new to Rails and my unfamiliarity with the framework showsUntil I have the free time to re-write this post, please bear with what is currently here and leave comments to help others that follow after you.</p></blockquote>
<p>Finally! I&#8217;ve been working on this little bit of Ruby code for hours now, all because of some odd bugs (which, when I learn Ruby better, will probably cease to become odd) and a lack of good Ruby on Rails documentation.</p>
<p>What I&#8217;ve been working on is getting two related HTML drop-down lists to update properly using the built-in Ajax support in Ruby on Rails. Specifically, when the user selects an Artist from the first drop-down list, only that artist&#8217;s Albums should be listed in the second. I wanted to do this without having to retrieve all of the albums during the initial page load and without having to do a post back when the user selects an artist. So, that&#8217;s where Ajax comes in. Ajax uses the JavaScript XMLHTTPRequest routine to retrieve new data from the server without requiring the user to refresh the webpage.</p>
<p align="center" style="text-align: center"><iframe scrolling="no" frameBorder="0" src="http://rcm.amazon.com/e/cm?t=roryonrails-20&amp;o=1&amp;p=8&amp;l=as1&amp;asins=0596527446&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=FFFFFF&amp;bg1=FFFFFF&amp;f=ifr" marginHeight="0" marginWidth="0" style="width: 120px; height: 240px; border: #cccccc 1px solid"></iframe></p>
<p>Some gotchas for the new Ruby programmer / web developer:</p>
<ul>
<li>You cannot modify the InnerHTML of a select drop-down list. Instead, you have to modify the InnerHTML of the div that _contains_ the select drop-down list, and when you do you have to recreate the whole drop-down.</li>
<li>When you are using instance variables (those that begin with the @-sign), ensure that the instance variable exists before trying to use:<br />
@variable += &#8220;value&#8221;<br />
syntax or else Ruby will throw up an error. For example, before that statement, add a line that reads:<br />
@variable = &#8220;&#8221;</li>
<li>When concatenating strings that contain instance variables, Ruby is picky in a way that I can&#8217;t fully describe. For example, the following does not work:<br />
@html += &#8220;&lt;option value=&#8217;&#8221; + @album.id + &#8220;&#8216;&gt;&#8221; + @album.album_name + &#8220;&lt;/option&gt;&#8221;<br />
But this does work:<br />
@html += &#8220;&lt;option value=&#8217;#{@album.id}&#8217;&gt;#{@album.album_name}&lt;/option&gt;&#8221;</li>
</ul>
<p>Anyway, this is the troubled code that _did not_ work:</p>
<p>admin_controller.rb<br />
<code>@albums = Album.find_all_by_artist_id(@params["artist_id"])<br />
@html = "&lt;select id='album_id' name='album_id'&gt;"<br />
@html += "&lt;option value=''&gt;No Album&lt;/option&gt;"<br />
@albums.each do |@album|<br />
Â Â Â @html += "&lt;option value='" + @album.id + "'&gt;" + @album.album_name + "&lt;/option&gt;"<br />
end<br />
@html += "&lt;/select&gt;"</code></p>
<p>After hours I got it working with this code:</p>
<p>admin_controller.rb<br />
<code>@albums = Album.find_all_by_artist_id(@params["artist_id"])<br />
@html = "&lt;select id='album_id' name='album_id'&gt;"<br />
@html += "&lt;option value=''&gt;No Album&lt;/option&gt;"<br />
@albums.each do |@album|<br />
Â Â Â @html += "&lt;option value='#{@album.id}'&gt;#{@album.album_name}&lt;/option&gt;"<br />
end<br />
@html += "&lt;/select&gt;"</code></p>
<p>But, this has to be coupled with the RHTML code in the template, as follows.</p>
<p>add_song.rhtml<br />
<code>&lt;%= javascript_include_tag "prototype" %&gt;</p>
<p>Artist<br />
&lt;select name="new_song[artist_id]" id="new_song[artist_id]"&gt;<br />
Â Â Â &lt;option value=""&gt;Select Artist&lt;/option&gt;<br />
Â Â Â &lt;% @artists.each do |artist| %&gt;<br />
Â Â Â Â Â Â &lt;option value="&lt;%= artist.id %&gt;"&gt;<br />
Â Â Â Â Â Â Â Â Â &lt;%= artist.first_name %&gt; &lt;%= artist.last_name %&gt;<br />
Â Â Â Â Â Â &lt;/option&gt;<br />
Â Â Â &lt;% end %&gt;<br />
&lt;/select&gt;</p>
<p>Album<br />
&lt;div id="album_id_container"&gt;<br />
Â Â Â &lt;select name="new_song[album_id]" disabled="disabled"&gt;<br />
Â Â Â Â Â Â &lt;option value=""&gt;No Album&lt;/option&gt;<br />
Â Â Â &lt;/select&gt;<br />
&lt;/div&gt;</p>
<p>&lt;%= observe_field("new_song[artist_id]",<br />
Â Â Â :frequency =&gt; 0.25,<br />
Â Â Â :update =&gt; "album_id_container",<br />
Â Â Â :url =&gt; { :action =&gt; :add_song_artist },<br />
Â Â Â :with =&gt; "'artist_id='+value") %&gt;</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.roryhansen.ca/2005/06/21/related-drop-down-lists-with-ruby-and-ajax/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
	</channel>
</rss>
