<?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>Erics Tech Blog &#187; Rails</title>
	<atom:link href="http://eric.lubow.org/category/ruby/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://eric.lubow.org</link>
	<description>Thoughts, musings, and other idealistic (sometimes useful) systems and development hoopla.</description>
	<lastBuildDate>Mon, 16 Aug 2010 12:30:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Model Specific Formatted Search Results Using Thinking Sphinx</title>
		<link>http://eric.lubow.org/2010/ruby/rails/model-specific-formatted-search-results-using-thinking-sphinx/</link>
		<comments>http://eric.lubow.org/2010/ruby/rails/model-specific-formatted-search-results-using-thinking-sphinx/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:15:23 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[sphinx]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=538</guid>
		<description><![CDATA[Having recently implemented Thinking Sphinx on one of my web sites, I thought it would be cool to be able to search every indexed model. With Thinking Sphinx, it&#8217;s easy to have a bunch of different classes returned in the results. The tougher part is displaying them in a way that is organized (although admittedly [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2010%2Fruby%2Frails%2Fmodel-specific-formatted-search-results-using-thinking-sphinx%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2010%2Fruby%2Frails%2Fmodel-specific-formatted-search-results-using-thinking-sphinx%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Having recently implemented <a href="http://freelancing-god.github.com/ts/en">Thinking Sphinx</a> on one of my web sites, I thought it would be cool to be able to search every indexed model.  With Thinking Sphinx, it&#8217;s easy to have a bunch of different classes returned in the results.  The tougher part is displaying them in a way that is organized (although admittedly not very DRY).<br />
<span id="more-538"></span><br />
Let&#8217;s start out with the controller method for search results (<strong>app/controllers/search_controller.rb</strong>):</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> result<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> !params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:model</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">blank</span>?<br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@model</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:model</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@model</span> = <span style="color:#996600;">&quot;ThinkingSphinx&quot;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@query</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:search</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:query</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@results</span> = Search.<span style="color:#9900CC;">model_search</span><span style="color:#006600; font-weight:bold;">&#40;</span>@model, <span style="color:#0066ff; font-weight:bold;">@query</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Also, let&#8217;s make sure we define the actual search model.  I define it in a separate search library (<strong>lib/search.rb</strong>).  This is the relevant snippet of code:</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">model_search</span><span style="color:#006600; font-weight:bold;">&#40;</span>model, keywords, var = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@search_options</span> = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> var<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:page</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">1</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:per_page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">15</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@search_options</span>.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:order</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;@relevance DESC&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:sort_mode</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:expr</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:sort_by</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;@weight * @weight&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
&nbsp; &nbsp; model.<span style="color:#9900CC;">constantize</span>.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span>keywords, <span style="color:#0066ff; font-weight:bold;">@search_options</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Assuming that we are working with the all models search, where the above model is <em>ThinkingSphinx</em>, let&#8217;s iterate over the search results with this code in the view (<strong>app/views/search/results.html.erb</strong>):</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@results</span>.<span style="color:#9900CC;">total_entries</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span>div id=<span style="color:#996600;">&quot;localLocationList&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>ul <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">&quot;list&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#0066ff; font-weight:bold;">@results</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>result<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>li<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= display_search_result<span style="color:#006600; font-weight:bold;">&#40;</span>result<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;/</span>li<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;/</span>ul<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>div <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">&quot;clear&quot;</span><span style="color:#006600; font-weight:bold;">&gt;&lt;/</span>div<span style="color:#006600; font-weight:bold;">&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;/</span>div<span style="color:#006600; font-weight:bold;">&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></div>
<p>And the view helper (<strong>app/helpers/search_helper.rb</strong>):</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">def</span> display_search_result<span style="color:#006600; font-weight:bold;">&#40;</span>result<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#CC0066; font-weight:bold;">eval</span> <span style="color:#996600;">&quot;render :partial =&gt; '#{result.class.to_s.downcase.pluralize}/result',<br />
&nbsp; &nbsp; :locals =&gt; { :result =&gt; result }&quot;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>The interesting thing about this bit of code is the <strong>eval</strong>.  The <strong>eval</strong> on each iteration decides which search result partial to display based on the class and then passes the result to the partial for display.  So if the result has a class of <em>Business</em>, the partial <strong>app/views/businesses/_result.html.erb</strong> will be rendered.  This is a quick example of a search result partial:</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;</span>span id=<span style="color:#996600;">&quot;localBizName&quot;</span><span style="color:#006600; font-weight:bold;">&gt;&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= <span style="color:#5A0A0A; font-weight:bold;">link_to</span> result.<span style="color:#9900CC;">name</span>, business_path<span style="color:#006600; font-weight:bold;">&#40;</span> result <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;&lt;/</span>span<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>br <span style="color:#006600; font-weight:bold;">/&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span>p <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">&quot;smallOffset&quot;</span><span style="color:#006600; font-weight:bold;">&gt;&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= truncate<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;#{result.description}&quot;</span>, :<span style="color:#5A0A0A; font-weight:bold;">length</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">128</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span></div></div>
<p>This is useful because all models don&#8217;t have the same characteristics.  By creating a search result partial for each model type, this can be reused for consistent looking search results around your webapp.  If the search is model specific, the same result partial will be used in every iteration over the results.  If the search is model agnostic, then you can display your search results in a consistent manner.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/seo/sortfix-or-a-next-generation-search-tool/' rel='bookmark' title='Permanent Link: Sortfix Or A Next Generation Search Tool'>Sortfix Or A Next Generation Search Tool</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/adding-an-average-column-to-a-model/' rel='bookmark' title='Permanent Link: Adding An Average Column To A Model'>Adding An Average Column To A Model</a></li>
<li><a href='http://eric.lubow.org/2008/databases/mysql/mysql-encoded-uri-search-and-replace/' rel='bookmark' title='Permanent Link: MySQL Encoded URI Search and Replace'>MySQL Encoded URI Search and Replace</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/ruby/rails/model-specific-formatted-search-results-using-thinking-sphinx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sitemaps On Rails</title>
		<link>http://eric.lubow.org/2009/seo/sitemaps-on-rails/</link>
		<comments>http://eric.lubow.org/2009/seo/sitemaps-on-rails/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 11:45:11 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[sitemap]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=473</guid>
		<description><![CDATA[SEO being an interest of mine, I couldn&#8217;t quite wrap my head around releasing a webapp without a sitemap. The problem is that there aren&#8217;t any really great sitemap plugins for Rails. Now I will grant that creating a sitemap in Rails is a challenging proposition and one that I would not like to undertake [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fseo%2Fsitemaps-on-rails%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fseo%2Fsitemaps-on-rails%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>SEO being an interest of mine, I couldn&#8217;t quite wrap my head around releasing a webapp without a sitemap.  The problem is that there aren&#8217;t any really <em>great</em> sitemap plugins for Rails.  Now I will grant that creating a sitemap in Rails is a challenging proposition and one that I would not like to undertake on my own unless absolutely necessary.  But I was hoping that there would be a rails sitemap &#8220;killer app&#8221; like there is with almost everything else in Rails.</p>
<p>So I dove in and tried a few options until I found one that worked.  First I wrote some code to generate an XML file and then created a sitemap_index.xml.gz file by hand.  This was very kludgy and definitely not a permanent solution.  I had also read suggestions about doing it in a sitemap_controller.rb file, but that seemed just as kludgy as using a view to generate the XML.  It was then time to explore the plugin world.<br />
<span id="more-473"></span><br />
After trying a few random plugins that I stumbled across, I finally found one that works for me. It&#8217;s call <a href="http://github.com/adamsalter/sitemap_generator">sitemap_generator</a> by Adam Salter.  What I really like about it is that it supports actually Ruby code in the <strong>config/sitemap.rb</strong> file.  Additionally, it solves a few common sitemap problems from the getgo (taken from the github README):<br />
    * Support for more than 50,000 urls (using a Sitemap Index file)<br />
    * Gzip of Sitemap files<br />
    * Variable priority of links<br />
    * Paging/sorting links (e.g. my_list?page=3)<br />
    * SSL host links (e.g. https:)<br />
    * Rails apps which are installed on a sub-path (e.g. example.com/blog_app/)</p>
<p>Using this plugin is easy.  Install it just as you would install any other plugin or gem (instructions are in the README).  Getting right into the sitemap, it&#8217;s really easy to accomplish whatever you want to do using regular Ruby code.  For example, if I simply want to find all the businesses on the site and add them to the sitemap, I can do this:</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Business.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>biz<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; sitemap.<span style="color:#5A0A0A; font-weight:bold;">add</span> business_path<span style="color:#006600; font-weight:bold;">&#40;</span>biz<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:lastmod</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> biz.<span style="color:#9900CC;">updated_at</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:priority</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0.9</span>, <span style="color:#ff3333; font-weight:bold;">:changefreq</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'monthly'</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>And if I have a slightly more complex URL structure like this one for posts (and who doesn&#8217;t have a posts model in their webapp):</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">http://foo.com/blog/2009/12/17/article-title</div></div>
<p>Then I can do something a little more complex like this:</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>post<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; sitemap.<span style="color:#5A0A0A; font-weight:bold;">add</span> show_path<span style="color:#006600; font-weight:bold;">&#40;</span>post.<span style="color:#9900CC;">named_route_generator</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:lastmod</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> post.<span style="color:#9900CC;">updated_at</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:priority</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0.7</span>, <span style="color:#ff3333; font-weight:bold;">:changefreq</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'monthly'</span><br />
&nbsp; <span style="color:#0066ff; font-weight:bold;">@comments</span> = Comment.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#996600;">&quot;post_id = ?&quot;</span>, post.<span style="color:#9900CC;">id</span> <span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>comment<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> !comment.<span style="color:#9900CC;">blank</span>?<br />
&nbsp; &nbsp; &nbsp; sitemap.<span style="color:#5A0A0A; font-weight:bold;">add</span> show_comment_path<span style="color:#006600; font-weight:bold;">&#40;</span> comment.<span style="color:#9900CC;">named_route_generator</span> <span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:lastmod</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> comment.<span style="color:#9900CC;">updated_at</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:priority</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0.4</span>, <span style="color:#ff3333; font-weight:bold;">:changefreq</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'monthly'</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>The only thing that I can&#8217;t seem to figure out if it supports is custom links in the URLs.  Meaning links that are fairly convoluted and (for some reason) not generated with a <em>link_to</em>, but have a custom link generator.  Other than that, you should probably go with sitemap_generator for your sitemap needs.</p>
<p>If you have a favorite Rails plugin for sitemaps, I would certainly like to hear about it.  And if you aren&#8217;t using sitemaps and are worried about SEO, then shame on you.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/ruby/rails/testing-sessions-with-digestsha256-passwords-in-rails/' rel='bookmark' title='Permanent Link: Testing Sessions with Digest::SHA256 Passwords In Rails'>Testing Sessions with Digest::SHA256 Passwords In Rails</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/seo/sitemaps-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Google Maps Marker With YM4R_GM</title>
		<link>http://eric.lubow.org/2009/ruby/rails/custom-google-maps-marker-with-ym4r_gm/</link>
		<comments>http://eric.lubow.org/2009/ruby/rails/custom-google-maps-marker-with-ym4r_gm/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 13:00:29 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[ym4r]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=466</guid>
		<description><![CDATA[In one my Rails applications, I allow the user to search for surrounding businesses from their current location. I always showed them a You Are Here marker. The issue I had with this was that the marker was always the icon as the search results. Differentiating these markers is actually extremely easy with ym4r_gm plugin. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fcustom-google-maps-marker-with-ym4r_gm%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fcustom-google-maps-marker-with-ym4r_gm%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In one my Rails applications, I allow the user to search for surrounding businesses from their current location.  I always showed them a <em>You Are Here</em> marker.  The issue I had with this was that the marker was always the icon as the search results.  Differentiating these markers is actually extremely easy with <a href="http://ym4r.rubyforge.org/">ym4r_gm plugin</a>.</p>
<p>First thing is to find a custom icon that you want to use.  You can just Google for custom Google maps icons.  I chose to use their default icon, just in a different blue. (You can download it <a href="http://eric.lubow.org/wp-content/uploads/2009/12/gmap_blue_icon.png">here</a> so you are working with what I am working with for this example).  The next thing I did was to use the Google <a href="http://www.powerhut.co.uk/googlemaps/custom_markers.php">custom markers web site</a> to find the proper config options for the icon.<br />
<span id="more-466"></span><br />
The Rails code is just used to create Javascript.  That is why it&#8217;s a good idea to run the image through custom markers website to get the Javascript output.  Ym4r_gm just turns the Ruby options specified into Javascript.  There are more options available, but this is the bare minimum necessary to create a separate icon.  All you need to do is use the option names given to you by the custom markers site and &#8220;Rubyify&#8221; them in <em>GIcon.new</em> (note: this is slightly harder than it sounds).</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#0066ff; font-weight:bold;">@map</span>.<span style="color:#9900CC;">icon_global_init</span><span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; GIcon.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> &nbsp;<span style="color:#ff3333; font-weight:bold;">:image</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;/images/gmap_blue_icon.png&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:icon_size</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> GSize.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">32</span>, <span style="color:#006666;">32</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:icon_anchor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> GPoint.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">16</span>, <span style="color:#006666;">32</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:info_window_anchor</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> GPoint.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">16</span>, <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">'youarehere_icon'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
youarehere_icon = Variable.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'youarehere_icon'</span><span style="color:#006600; font-weight:bold;">&#41;</span> &nbsp; &nbsp;<br />
<span style="color:#0066ff; font-weight:bold;">@map</span>.<span style="color:#9900CC;">center_zoom_init</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">lat</span>, <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">lng</span> <span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006666;">15</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
youarehere = GMarker.<span style="color:#5A0A0A; font-weight:bold;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">lat</span>, <span style="color:#0066ff; font-weight:bold;">@location</span>.<span style="color:#9900CC;">lng</span> <span style="color:#006600; font-weight:bold;">&#93;</span>,<br />
&nbsp; <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;You Are Here&quot;</span>,<br />
&nbsp; <span style="color:#ff3333; font-weight:bold;">:info_window</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;You Are Here&quot;</span>,<br />
&nbsp; <span style="color:#ff3333; font-weight:bold;">:icon</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> youarehere_icon,<br />
&nbsp; :<span style="color:#5A0A0A; font-weight:bold;">draggable</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span><br />
<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<br />
<span style="color:#0066ff; font-weight:bold;">@map</span>.<span style="color:#9900CC;">declare_init</span><span style="color:#006600; font-weight:bold;">&#40;</span> youarehere, <span style="color:#996600;">'youarehere'</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#0066ff; font-weight:bold;">@map</span>.<span style="color:#9900CC;">overlay_init</span><span style="color:#006600; font-weight:bold;">&#40;</span> youarehere <span style="color:#006600; font-weight:bold;">&#41;</span></div></div>
<p>A few things worth nothing about this code in order to better understand what it&#8217;s doing.</p>
<p>If you don&#8217;t have the <strong>icon_size</strong> set properly, then your icon will look funny when the map renders.  The <strong>icon_anchor</strong> and <strong>info_window_anchor</strong> are just coincidentally half of 32, this is not always the case.  And that last item in <em>GIcon.new</em> is what is going to be the Javascript variable name.  For sanity&#8217;s sake, I chose to keep the Ruby variable name and Javascript variable names consistent.  The <em>Variable.new</em> line creates a ym4r_gm icon object in Ruby to be  passed to the <em>:icon</em> parameter of the <em>youarehere</em> marker.</p>
<p>The rest of the code is similar to your regular ym4r_gm map icon stuff.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/mail/transferring-email-from-gmailgoogle-apps-to-dovecot-with-larch/' rel='bookmark' title='Permanent Link: Transferring Email From Gmail/Google Apps to Dovecot With Larch'>Transferring Email From Gmail/Google Apps to Dovecot With Larch</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/one-time-modal-windows-with-rails-and-fancybox/' rel='bookmark' title='Permanent Link: One Time Modal Windows With Rails and Fancybox'>One Time Modal Windows With Rails and Fancybox</a></li>
<li><a href='http://eric.lubow.org/2009/misc/getting-rid-of-the-google-analytics-overlay/' rel='bookmark' title='Permanent Link: Getting Rid of The Google Analytics Overlay'>Getting Rid of The Google Analytics Overlay</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/ruby/rails/custom-google-maps-marker-with-ym4r_gm/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Country-State Select Using Carmen and jQuery</title>
		<link>http://eric.lubow.org/2009/ruby/rails/country-state-select-using-carmen-and-jquery/</link>
		<comments>http://eric.lubow.org/2009/ruby/rails/country-state-select-using-carmen-and-jquery/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 11:45:23 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=450</guid>
		<description><![CDATA[I&#8217;ve been wanting to find a way to use drop down menus for countries and their states when they exist. But keeping a list on my own would have been a huge pain in the ass. So rather reinvent the wheel, I found the Carmen plugin for Rails. All I have to do is keep [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fcountry-state-select-using-carmen-and-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fcountry-state-select-using-carmen-and-jquery%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>I&#8217;ve been wanting to find a way to use drop down menus for countries and their states when they exist.  But keeping a list on my own would have been a huge pain in the ass.  So rather reinvent the wheel, I found the <a href="http://github.com/jim/carmen">Carmen plugin</a> for Rails.  All I have to do is keep the plugin updated and my list of countries and their states will be kept updated as well.</p>
<p>How do I do all this with unobtrusive Javascript and Rails you ask? Good question. Let me show you.  Don&#8217;t forget to install the plugin (or use the gem).</p>
<p>Let&#8217;s start out by adding the drop down menu to our view.  In my case I have it in a partial for the address part of a form.  You&#8217;ll have to modify this slightly to pick up the values of the form if the partial handles edits as well.  This one is just for a <em>new</em> method as it uses a default country of US and its states.  Note the div ID here of <em>addressStates</em>; we will be using this later in the javascript.<br />
<span id="more-450"></span></p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#5A0A0A; font-weight:bold;">fields_for</span> <span style="color:#996600;">&quot;address&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>addr_form<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span>p<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; Country<span style="color:#006600; font-weight:bold;">&lt;</span>br <span style="color:#006600; font-weight:bold;">/&gt;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= addr_form.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#ff3333; font-weight:bold;">:country</span>, Carmen::country_names, <span style="color:#006600; font-weight:bold;">&#123;</span>:selected <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'United States'</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span>div id=<span style="color:#996600;">&quot;addressStates&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'addresses/states'</span>,<br />
&nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:locals</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:states <span style="color:#006600; font-weight:bold;">=&gt;</span> Carmen::states<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'US'</span><span style="color:#006600; font-weight:bold;">&#41;</span>, :<span style="color:#5A0A0A; font-weight:bold;">form</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> addr_form<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;/</span>div<span style="color:#006600; font-weight:bold;">&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></div>
<p>Since we are rendering a partial, we should add that partial as well.  Create the file <strong>app/views/addresses/_states.html.erb</strong> and add the code below.  You&#8217;ll notice that if no set of states exist in the country selected, I render a text_field instead.  This is obviously optional and could potentially lead to issues if the list of states for a particular country is updated and the values in your DB don&#8217;t match.</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&lt;</span>p<span style="color:#006600; font-weight:bold;">&gt;</span>State <span style="color:#9966CC; font-weight:bold;">or</span> Territory<span style="color:#006600; font-weight:bold;">&lt;</span>br <span style="color:#006600; font-weight:bold;">/&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#5A0A0A; font-weight:bold;">fields_for</span> <span style="color:#996600;">&quot;address&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>addr_form<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">if</span> !states.<span style="color:#9900CC;">blank</span>? <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= addr_form.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#ff3333; font-weight:bold;">:state</span>, states.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>s<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#91;</span>s<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>,s<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:prompt <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Select State'</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span>:style <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'width: 180px'</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= addr_form.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:state</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span></div></div>
<p>Now, let&#8217;s update our <strong>application.js</strong> file.  Because of the way the form is laid out, my element ID works out to be <strong>address_country</strong>.  Make sure you use the correct element ID otherwise the action will not fire. (Thanks to <a href="http://developingwithstyle.com/">Joel</a> for helping with the javascript).</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Select the state after the country has been selected</span><br />
jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'select#address_country'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> country <span style="color: #339933;">=</span> <span style="color: #3366CC;">'country='</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'select#address_country :selected'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; jQuery.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/addresses/update_state_select?'</span> <span style="color: #339933;">+</span> country<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div#addressStates&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&lt;script type='text/javascript'&gt;&quot;</span> <span style="color: #339933;">+</span> data <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/script&gt;&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>You&#8217;ll notice that when we populate the div, we wrap it in javascript &lt;script&gt; tags.  This is because the response we get is an AJAX response.  In order for the browser to render it properly, we need to wrap it in script tags.</p>
<p>Before the above Javascript will work, we have to setup a route specifically to handle this request.  So add the following code to your <strong>config/routes.rb</strong>.  I chose to put this in the addresses controller (since it&#8217;s a functional part of an address, it just seemed logical), but feel free to put it wherever you see fit.  Just make sure you then add the method to the proper controller.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">map.<span style="color:#9900CC;">states</span> <span style="color:#996600;">'/addresses/update_state_select'</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'addresses'</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'update_state_select'</span></div></div>
<p>Now we need to add the method to our controller. As I said, I chose the <em>addresses</em> controller in my project because its logical.  So I opened up the <strong>app/controllers/addresses_controller.rb</strong> and added the following code.</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">def</span> update_state_select<br />
&nbsp; states = Carmen::states<span style="color:#006600; font-weight:bold;">&#40;</span> Carmen::country_code<span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:country</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#5A0A0A; font-weight:bold;">render</span> :<span style="color:#5A0A0A; font-weight:bold;">update</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; page.<span style="color:#5A0A0A; font-weight:bold;">replace_html</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;div#addressStates&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;states&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:locals</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:states</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> states <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>And there you have it.  A straightforward way to keep your list of countries and states updated and consistent.  Hope this works for you as well as it does for me.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/ruby/rails/checking-roles-in-views-using-rolerequirement/' rel='bookmark' title='Permanent Link: Checking Roles in Views Using RoleRequirement'>Checking Roles in Views Using RoleRequirement</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/one-time-modal-windows-with-rails-and-fancybox/' rel='bookmark' title='Permanent Link: One Time Modal Windows With Rails and Fancybox'>One Time Modal Windows With Rails and Fancybox</a></li>
<li><a href='http://eric.lubow.org/2010/ruby/rails/model-specific-formatted-search-results-using-thinking-sphinx/' rel='bookmark' title='Permanent Link: Model Specific Formatted Search Results Using Thinking Sphinx'>Model Specific Formatted Search Results Using Thinking Sphinx</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/ruby/rails/country-state-select-using-carmen-and-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding AJAX Bookmarks to Your Rails Application (Part 2 of 2)</title>
		<link>http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-2-of-2/</link>
		<comments>http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-2-of-2/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 12:00:35 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=404</guid>
		<description><![CDATA[In part 1 of this series, we discussed the base models, controller, database migrations necessary to get this project off the ground. Now we are going to continue with this functionality Let&#8217;s take a look at what needs to go into the models to support this. If you have a model that uses a slug [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fadding-ajax-bookmarks-to-your-rails-application-part-2-of-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fadding-ajax-bookmarks-to-your-rails-application-part-2-of-2%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In <a href="http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-1-of-2/">part 1</a> of this series, we discussed the base models, controller, database migrations necessary to get this project off the ground.  Now we are going to continue with this functionality</p>
<p>Let&#8217;s take a look at what needs to go into the models to support this.  If you have a model that uses a slug generated via <em>to_param</em>, then your code will look like the top model, If you are using the normal numeric id convention, then it will look like the bottom model.  The reason for the specifically named methods <em>get_title</em> and <em>get_description</em> will become apparent when you start displaying bookmarks.  The thought process is that you can use a consistent set of calls for displaying the bookmark information and put the code to grab that information in the model where it belongs rather than loading up the helper methods.  What should also be noted is that the <strong>title</strong> and <strong>description</strong> fields are not always consistent across models.  Therefore the method naming conventions returns the proper column with consistent method names.<br />
<span id="more-404"></span><br />
Slug based model:</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">get_title</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; find_by_id<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">name</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">get_description</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; find_by_id<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">description</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_id_by_site_url</span><span style="color:#006600; font-weight:bold;">&#40;</span>site_url<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; url = site_url.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; find_by_slug<span style="color:#006600; font-weight:bold;">&#40;</span>url<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">id</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Numeric ID based model:</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">get_title</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; find_by_id<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">title</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">get_description</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; find_by_id<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">description</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_id_by_site_url</span><span style="color:#006600; font-weight:bold;">&#40;</span>site_url<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; site_url.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Now that we have the code in our models, controllers, and the database is setup, let&#8217;s put it into our views so we can actually take advantage of our bookmark system.  Let&#8217;s dive right in to adding the code.  Inside our show view, we just add the lines below.  We check to see if the user is logged in.  If they are, check their bookmark status of this page and show the appropriate image.</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt; % if current_user %&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/bookmarks/toggle?url=&lt;%=h request.request_uri %&gt;</span></span>&quot; id=&quot;bookmark_toggle&quot; class=&quot;ajax_get&quot;&gt;<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt; % if bookmarked?<span style="color: #66cc66;">&#40;</span>current_user.<span style="color: #000066;">id</span>, request.request_uri<span style="color: #66cc66;">&#41;</span> %&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt; %<span style="color: #66cc66;">=</span> image_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/images/star_bookmark_enabled.png&quot;</span>, :<span style="color: #000066;">id</span> <span style="color: #66cc66;">=</span>&gt;</span> &quot;bookmark_star&quot;, :size =&gt; &quot;16x16&quot;, :alt =&gt; &quot;Bookmark&quot;, :class =&gt; &quot;bookmark&quot;, :border =&gt; 0) %&gt;<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt; % else %&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt; %<span style="color: #66cc66;">=</span> image_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/images/star_bookmark_disabled.png&quot;</span>, :<span style="color: #000066;">id</span> <span style="color: #66cc66;">=</span>&gt;</span> &quot;bookmark_star&quot;, :size =&gt; &quot;16x16&quot;, :alt =&gt; &quot;Bookmark&quot;, :class =&gt; &quot;bookmark&quot;, :border =&gt; 0) %&gt;<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt; % end %&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a>&gt;</span><br />
<span style="color: #009900;">&lt; % end %&gt;</span></div></div>
<p>The are a few helper methods that are required here to support this functionality.  The bookmarks_helper.rb looks as follows.  Notice the use of the <em>send</em> method in the <em>link_to_bookmark</em> method.  That enables us to abstract the <em>link_to</em> to be for any model_path that you have in the <strong>Bookmarktypes</strong>.</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">module</span> BookmarksHelper<br />
<br />
&nbsp; <span style="color:#008000; font-style:italic;"># Make sure that self.find_id_by_site_url is in every model that can be bookmarked</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> bookmarked?<span style="color:#006600; font-weight:bold;">&#40;</span>user_id,request_uri<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; uri_array = request_uri.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; bookmark_type_id = Bookmarktype.<span style="color:#9900CC;">find_by_model</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri_array<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">singularize</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">id</span><br />
&nbsp; &nbsp; modeltype = Bookmarktype.<span style="color:#9900CC;">find_by_model</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri_array<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">singularize</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">model</span>.<span style="color:#9900CC;">capitalize</span>.<span style="color:#9900CC;">constantize</span><br />
&nbsp; &nbsp; model_id = modeltype.<span style="color:#9900CC;">find_id_by_site_url</span><span style="color:#006600; font-weight:bold;">&#40;</span>request_uri<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> Bookmark.<span style="color:#9900CC;">is_bookmarked</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>user_id,bookmark_type_id,model_id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> print_bookmark_type<span style="color:#006600; font-weight:bold;">&#40;</span>model_type_id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; Bookmarktype.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>model_type_id<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">model</span>.<span style="color:#9900CC;">capitalize</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#008000; font-style:italic;"># Make sure that self.get_title is in every model that can be bookmarked</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> link_to_bookmark<span style="color:#006600; font-weight:bold;">&#40;</span>anchor_text,bookmark<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; controller = Bookmarktype.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>bookmark.<span style="color:#9900CC;">model_type_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">model</span>.<span style="color:#9900CC;">pluralize</span><br />
&nbsp; &nbsp; title = Bookmarktype.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>bookmark.<span style="color:#9900CC;">model_type_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">model</span>.<span style="color:#9900CC;">capitalize</span>.<span style="color:#9900CC;">constantize</span>.<span style="color:#9900CC;">get_title</span><span style="color:#006600; font-weight:bold;">&#40;</span>bookmark.<span style="color:#9900CC;">model_id</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@item</span> = Bookmarktype.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>bookmark.<span style="color:#9900CC;">model_type_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">model</span>.<span style="color:#9900CC;">capitalize</span>.<span style="color:#9900CC;">constantize</span>.<span style="color:#9900CC;">find_by_id</span><span style="color:#006600; font-weight:bold;">&#40;</span>bookmark.<span style="color:#9900CC;">model_id</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#5A0A0A; font-weight:bold;">link_to</span> <span style="color:#996600;">&quot;#{anchor_text}. #{title}&quot;</span>, send<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{controller.singularize}_path&quot;</span>, <span style="color:#0066ff; font-weight:bold;">@item</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; <br />
&nbsp; <span style="color:#008000; font-style:italic;"># Make sure that self.get_description is in every model that can be bookmarked</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> print_bookmark_desc<span style="color:#006600; font-weight:bold;">&#40;</span>bookmark<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; Bookmarktype.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>bookmark.<span style="color:#9900CC;">model_type_id</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">model</span>.<span style="color:#9900CC;">capitalize</span>.<span style="color:#9900CC;">constantize</span>.<span style="color:#9900CC;">get_description</span><span style="color:#006600; font-weight:bold;">&#40;</span>bookmark.<span style="color:#9900CC;">model_id</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>You&#8217;ll recognize some of this code from the model and the controller.  I know this isn&#8217;t necessarily DRY, but I am by no means a Rails expert.  But I do know this code works.</p>
<p>The last item to tie this whole thing together is allowing the user to view their bookmarks.  We already have the code for the show page in our controller.  Keeping that in mind, let&#8217;s take a look at using that and our helper methods to give a decent presentation of our bookmarks.</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;middle_back&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>To bookmark a page or remove a bookmark, just click the star.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt; %<span style="color: #66cc66;">=</span> image_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/images/star_bookmark_enabled.png&quot;</span>, :<span style="color: #000066;">size</span> <span style="color: #66cc66;">=</span>&gt;</span> &quot;16x16&quot;, :alt =&gt; &quot;Bookmark&quot;, :border =&gt; 0) %&gt; Bookmarked<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt; %<span style="color: #66cc66;">=</span> image_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/images/star_bookmark_disabled.png&quot;</span>, :<span style="color: #000066;">size</span> <span style="color: #66cc66;">=</span>&gt;</span> &quot;16x16&quot;, :alt =&gt; &quot;Bookmark&quot;, :border =&gt; 0) %&gt; Not Bookmarked<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt; % if !@bookmarks.blank? %&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt; % count <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> %&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Below are the pages that you have bookmarked.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt; % @bookmarks.each do |bookmark| %&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;horizontal_line&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bookmark_list&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt; % count <span style="color: #66cc66;">=</span> count + <span style="color: #cc66cc;">1</span> %&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">span</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bookmarkLink&quot;</span>&gt;&lt; %<span style="color: #66cc66;">=</span> link_to_bookmark<span style="color: #66cc66;">&#40;</span>count, bookmark<span style="color: #66cc66;">&#41;</span> %&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">span</span></a>&gt;&lt;<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">span</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bookmarkType&quot;</span>&gt;&lt; %<span style="color: #66cc66;">=</span> print_bookmark_type<span style="color: #66cc66;">&#40;</span>bookmark.model_type_id<span style="color: #66cc66;">&#41;</span> %&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">span</span></a>&gt;&lt;<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">span</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bookmarkDescription&quot;</span>&gt;&lt; %<span style="color: #66cc66;">=</span> print_bookmark_desc<span style="color: #66cc66;">&#40;</span>bookmark<span style="color: #66cc66;">&#41;</span> %&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">span</span></a>&gt;&lt;<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">span</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bookmarkAddedDate&quot;</span>&gt;</span>Added <span style="color: #009900;">&lt; %<span style="color: #66cc66;">=</span> bookmark.created_at %&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">span</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt; % end %&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;horizontal_line&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt; % else %&gt;</span><br />
&nbsp; &nbsp; &nbsp; You currently have no items bookmarked.<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt; % end %&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span></div></div>
<p>And there you have it.  Your users can now bookmark pages and view their list of bookmarks in a profile type setting. There might be other ways to do this, but this way has worked for me thus far.   Good luck.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-1-of-2/' rel='bookmark' title='Permanent Link: Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)'>Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)</a></li>
<li><a href='http://eric.lubow.org/2010/ruby/rails/model-specific-formatted-search-results-using-thinking-sphinx/' rel='bookmark' title='Permanent Link: Model Specific Formatted Search Results Using Thinking Sphinx'>Model Specific Formatted Search Results Using Thinking Sphinx</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/shortcut-notation-in-rails-callbacks/' rel='bookmark' title='Permanent Link: Shortcut Notation in Rails Callbacks'>Shortcut Notation in Rails Callbacks</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-2-of-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)</title>
		<link>http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-1-of-2/</link>
		<comments>http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-1-of-2/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 11:45:57 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=371</guid>
		<description><![CDATA[It you want to add the ability to bookmark pages in your Rails application, its actually a fairly straightforward thing to do. You can even do them in AJAX. There may be better ways to do this, but this way is somewhat abstract and it works for me, so hopefully it can work for you [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fadding-ajax-bookmarks-to-your-rails-application-part-1-of-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fadding-ajax-bookmarks-to-your-rails-application-part-1-of-2%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>It you want to add the ability to bookmark pages in your Rails application, its actually a fairly straightforward thing to do.  You can even do them in AJAX.  There may be better ways to do this, but this way is somewhat abstract and it works for me, so hopefully it can work for you too.  It is abstract in the sense that it will work for models with different URL styles and different column names.</p>
<p>The way this works is that you add a bookmark icon (which is initially disabled) to a show &lt;model_name&gt; page.  When the user clicks on the bookmark icon, an AJAX  query will be made in the background and update the users bookmark lists.  I am approaching this from an abstract methodology.  Meaning that I have &#8220;forced&#8221; these methods to work with models executed in various fashions (as I give examples of below). The AJAX call is going to be simply work as a toggle.  It will actually call a toggle method in the bookmarks controller and change the current value and replace the image. The user can then view the pages they have bookmarked in their profile.</p>
<p>I have decided to break this into a multi-part blog entry because it ends up being quite long.  Not necessarily in how long it takes, just the amount of space it takes to show all the code.  I have done my best to only show relevant code and maintain brevity.  Note: I will not cover how to allow for unobtrusive AJAX calls.  That is beyond the scope of this set of posts.<br />
 <span id="more-371"></span><br />
I chose the following images to represent this.  The empty blue star signifies that a bookmark is disabled.  The filled in blue star represents the fact that the page is bookmarked by the current user.  You can go to Google images or anywhere else to find enabled/disabled images.  I just chose these particular images because they matched my site.</p>
<table align="center">
<tr>
<td style="padding-right: 20px;"><div id="attachment_375" class="wp-caption alignleft" style="width: 74px"><a href="http://eric.lubow.org/wp-content/uploads/2009/11/star_bookmark_enabled.png"><img src="http://eric.lubow.org/wp-content/uploads/2009/11/star_bookmark_enabled.png" alt="Star Bookmark Enabled" title="Star Bookmark Enabled" width="64" height="64" class="size-full wp-image-375" /></a><p class="wp-caption-text">Star Bookmark Enabled</p></div></td>
<td style="padding-left: 20px;"><div id="attachment_374" class="wp-caption alignnone" style="width: 74px"><a href="http://eric.lubow.org/wp-content/uploads/2009/11/star_bookmark_disabled.png"><img src="http://eric.lubow.org/wp-content/uploads/2009/11/star_bookmark_disabled.png" alt="Star Bookmark Disabled" title="Star Bookmark Disabled" width="64" height="64" class="size-full wp-image-374" /></a><p class="wp-caption-text">Star Bookmark Disabled</p></div></td>
</tr>
</table>
<p>The first thing you need to do is to create the migrations for bookmarks and bookmarktypes.  Make sure the bookmarktypes exist for every model that you want to allow to be bookmarked and are the singularized lowercase version of the model name (e.g. Users -> user, Businesses -> business).</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> AddBookmarks <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span></span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Note we are setting no primary key here intentionally</span><br />
&nbsp; &nbsp; create_table <span style="color:#ff3333; font-weight:bold;">:bookmarks</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:id <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:force</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> &nbsp;<span style="color:#ff3333; font-weight:bold;">:user</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">integer</span> &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:model_type_id</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">integer</span> &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:model_id</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">timestamps</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; create_table <span style="color:#ff3333; font-weight:bold;">:bookmarktypes</span>, <span style="color:#ff3333; font-weight:bold;">:force</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">string</span> &nbsp; &nbsp;<span style="color:#ff3333; font-weight:bold;">:model</span><br />
&nbsp; &nbsp; &nbsp; t.<span style="color:#9900CC;">timestamps</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; Bookmarktype.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:model</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'business'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; Bookmarktype.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:model</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'singularlowercasemodelname'</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span><br />
&nbsp; &nbsp; drop_table <span style="color:#ff3333; font-weight:bold;">:bookmarktypes</span><br />
&nbsp; &nbsp; drop_table <span style="color:#ff3333; font-weight:bold;">:bookmarks</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Here we are adding the bookmark and bookmarktype model.</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> Bookmark <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span><br />
&nbsp; <span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> &nbsp;<span style="color:#ff3333; font-weight:bold;">:user</span><br />
&nbsp; <span style="color:#5A0A0A; font-weight:bold;">has_one</span> &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:bookmarktype</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_bookmark</span><span style="color:#006600; font-weight:bold;">&#40;</span>user_id,model_type_id,model_id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; find<span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; :<span style="color:#5A0A0A; font-weight:bold;">first</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;user_id = ? AND model_type_id = ? AND model_id = ?&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;user_id, model_type_id, model_id<span style="color:#006600; font-weight:bold;">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">is_bookmarked</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>user_id,model_type_id,model_id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; find_bookmark<span style="color:#006600; font-weight:bold;">&#40;</span>user_id, model_type_id, model_id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> Bookmarktype <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span><br />
&nbsp; <span style="color:#5A0A0A; font-weight:bold;">validates_presence_of</span> <span style="color:#ff3333; font-weight:bold;">:model</span>, <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;can't be blank&quot;</span> &nbsp;<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Once the models and migrations are complete, then we can go ahead and add the controller code.  Create a bookmarks controller.  This will contain the toggle method that will be doing the majority of the legwork.  You&#8217;ll notice that the <em>toggle</em> method below uses a url param.  This works in the following way:</p>
<ol>
<li>When the AJAX link is clicked on the model show view page, it will send an AJAX request to bookmarks controller that looks like this:
<ul>
<li>http://website/bookmarks/toggle?url=/businesses/mybusiness&#038;_=1258315454002</li>
<li>http://localhost/bookmarks/toggle?url=/user/1234&#038;_=1258315600823</li>
</ul>
<p>    The _ param is simply the authenticity token put into a GET string.</li>
<li>The URL parameter will be broken down into an array:
<ul>
<li>[0]: empty  &nbsp;  OR  &nbsp;  empty</li>
<li>[1]: businesses  &nbsp;  OR  &nbsp;  user</li>
<li>[2]: mybusiness  &nbsp;  OR  &nbsp;  1234</li>
</ul>
</li>
<li>We get the modeltype (which will soon act just like a class like you would call Business.find or User.find).  We do this by capitalizing it and constantizing it.  We capitalize it so it looks like the model class name otherwise we generate a nomethod error.  Constantizing the model name turns it into something that is usable as a class: <em>Bookmarktype.find_by_model(uri_array[1].singularize).model.capitalize.constantize</em></li>
<li>This is where it gets interesting and you get to deal with multiple types of URLs in a model: <em>modeltype.find_id_by_site_url(request_uri)</em>.  The <em>find_id_by_site_url</em> method is a method that requires definition in each model because it can be different in each model.  We will cover this later.</li>
<li>Then we check to see if the bookmark exists by calling the find method we wrote in our Bookmark model.  We use the result to determine whether the bookmark should be added or removed in the actual toggle process.</li>
<li>The bookmark status is toggled in the database, either added or deleted.  The <em>delete_all</em> method is used because there is no way in Rails (that I could find) to delete a row with no primary key.</li>
<li>Finally, the Javascript is rendered so nothing happens to the original calling page.</li>
</ol>
<p>And finally, here is the actual bookmarks controller.  We will go through the usage of the <em>show</em> method later.</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#9966CC; font-weight:bold;">class</span> BookmarksController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController<br />
&nbsp; <span style="color:#5A0A0A; font-weight:bold;">layout</span> <span style="color:#996600;">'home'</span><br />
&nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">show</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user_id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@bookmarks</span> = Bookmark.<span style="color:#9900CC;">find_all_by_user_id</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user_id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">paginate</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:page</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:per_page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">toggle</span><br />
&nbsp; &nbsp; uri_array = <span style="color:#CC00FF; font-weight:bold;">CGI</span>::unescape<span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:url</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; bookmark_type_id = Bookmarktype.<span style="color:#9900CC;">find_by_model</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri_array<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">singularize</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">id</span><br />
&nbsp; &nbsp; modeltype = Bookmarktype.<span style="color:#9900CC;">find_by_model</span><span style="color:#006600; font-weight:bold;">&#40;</span>uri_array<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">singularize</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">model</span>.<span style="color:#9900CC;">capitalize</span>.<span style="color:#9900CC;">constantize</span><br />
&nbsp; &nbsp; model_id = modeltype.<span style="color:#9900CC;">find_id_by_site_url</span><span style="color:#006600; font-weight:bold;">&#40;</span>request_uri<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@bookmark</span> = Bookmark.<span style="color:#9900CC;">find_bookmark</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_skydiver.<span style="color:#9900CC;">id</span>,bookmark_type_id,model_id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># We use a delete_all here because there is no other way in Rails to delete a row from a table without a PRIMARY KEY id</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0066ff; font-weight:bold;">@bookmark</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; Bookmark.<span style="color:#9900CC;">delete_all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;user_id = ? AND model_type_id = ? AND model_id = ?&quot;</span>, current_user.<span style="color:#9900CC;">id</span>, bookmark_type_id, model_id<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">else</span><br />
&nbsp; &nbsp; &nbsp; Bookmark.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:user_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> current_user.<span style="color:#9900CC;">id</span>, <span style="color:#ff3333; font-weight:bold;">:model_type_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> bookmark_type_id, <span style="color:#ff3333; font-weight:bold;">:model_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> model_id<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color:#5A0A0A; font-weight:bold;">respond_to</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">js</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#5A0A0A; font-weight:bold;">render</span> :<span style="color:#5A0A0A; font-weight:bold;">layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Lastly on the AJAX side of this, we need to put response view together.  This is a very simple line of code in the <strong>toggle.js.erb</strong>:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">swapBookmarkImage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>And for the unobtrusive aspect of Javascript and in the interest of keeping Javascript all in the same place, add that function to your <strong>application.js</strong>:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> swapBookmarkImage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> origSrc <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img#bookmark_star&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Change the image src toggle based on the current image</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> origSrc <span style="color: #339933;">==</span> <span style="color: #3366CC;">'/images/star_bookmark_disabled.png'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> newSrc <span style="color: #339933;">=</span> <span style="color: #3366CC;">'/images/star_bookmark_enabled.png'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> newSrc <span style="color: #339933;">=</span> <span style="color: #3366CC;">'/images/star_bookmark_disabled.png'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Swap out the image</span><br />
&nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img#bookmark_star&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span> newSrc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Click <a href="http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-2-of-2/">here</a> for part 2 of this series.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-2-of-2/' rel='bookmark' title='Permanent Link: Adding AJAX Bookmarks to Your Rails Application (Part 2 of 2)'>Adding AJAX Bookmarks to Your Rails Application (Part 2 of 2)</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/one-time-modal-windows-with-rails-and-fancybox/' rel='bookmark' title='Permanent Link: One Time Modal Windows With Rails and Fancybox'>One Time Modal Windows With Rails and Fancybox</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/shortcut-notation-in-rails-callbacks/' rel='bookmark' title='Permanent Link: Shortcut Notation in Rails Callbacks'>Shortcut Notation in Rails Callbacks</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-1-of-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>One Time Modal Windows With Rails and Fancybox</title>
		<link>http://eric.lubow.org/2009/ruby/rails/one-time-modal-windows-with-rails-and-fancybox/</link>
		<comments>http://eric.lubow.org/2009/ruby/rails/one-time-modal-windows-with-rails-and-fancybox/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 11:00:26 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=331</guid>
		<description><![CDATA[Let&#8217;s say that you have a situation that you want to have a modal window show up only once for each user. It&#8217;s actually not that difficult although lots of Googling around got me nowhere. I am choosing to use FancyBox for my modal window, but feel free to use your modal framework of choice. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fone-time-modal-windows-with-rails-and-fancybox%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fone-time-modal-windows-with-rails-and-fancybox%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Let&#8217;s say that you have a situation that you want to have a modal window show up only once for each user.  It&#8217;s actually not that difficult although lots of Googling around got me nowhere.  I am choosing to use <a href="http://fancybox.net/">FancyBox</a> for my modal window, but feel free to use your modal framework of choice.  So let&#8217;s get down to business.</p>
<p>First thing you&#8217;ll need to do is download <a href="http://fancybox.net/">FancyBox</a> and copy the stylesheets, images, and Javascript files to their proper/desired location in your Rails app.  Style the window according to your likings.</p>
<p>Whether it is right or wrong, I did this entirely in the view, without even pulling the Javascript out into the application.js (or even another Javascript file for that matter).  My reason was that I only want the modal window showing up on this page.  If you want your modal window to show up somewhere else (or on every page), then put the code in your layout.  But remember that this call will be executed every time the page loads. I put mine in a profile page which doesn&#8217;t get accessed that often so the conditional is not checked quite as frequently.</p>
<p>My application uses Facebook Connect and grabs the users Facebook Proxy email address (FB app developers will know what this is).  So I check if that&#8217;s the email I have for the user.  If yes, then I pop up a modal window on page load only once to get their regular email address and possibly a password so they can login to their account without Facebook Connect if they want.  When the modal window is shown, a variable is set in the cookie (note that this cookie is shared with authlogic for sessions) to ensure that the modal window isn&#8217;t shown again.<br />
<span id="more-331"></span><br />
The explanation of the code is below with comments in the code.</p>
<p>The code that goes in the view is as follows:</p>
<div class="codecolorer-container rails default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="rails codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;"># Check the user's email and see if they have a facebook proxy email address</span><br />
<span style="color:#008000; font-style:italic;"># Check the user's cookie to see if they have already seen the Facebook modal window</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">email</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^.<span style="color:#006600; font-weight:bold;">+</span>proxymail.<span style="color:#9900CC;">facebook</span>.<span style="color:#9900CC;">com</span>$<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">and</span> <span style="color:#006600; font-weight:bold;">&#40;</span>cookies<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:facebook_email_modal_shown</span><span style="color:#006600; font-weight:bold;">&#93;</span> != <span style="color:#996600;">&quot;1&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Include the JS and stylesheets</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= <span style="color:#5A0A0A; font-weight:bold;">javascript_include_tag</span> <span style="color:#996600;">'jquery.fancybox-1.2.3'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= <span style="color:#5A0A0A; font-weight:bold;">stylesheet_link_tag</span> <span style="color:#996600;">'jquery.fancybox'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>script type=<span style="color:#996600;">&quot;text/javascript&quot;</span> charset=<span style="color:#996600;">&quot;utf-8&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">/*</span> Function to <span style="color:#CC0066; font-weight:bold;">load</span> the modal window by clicking the <span style="color:#996600;">&quot;invisible&quot;</span> link <span style="color:#006600; font-weight:bold;">*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var modalLoadFunction = function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a.modalGroup&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">click</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearInterval<span style="color:#006600; font-weight:bold;">&#40;</span>globalInterval<span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">/*</span> Hide the div on <span style="color:#CC0066; font-weight:bold;">load</span> &nbsp;<span style="color:#9966CC; font-weight:bold;">and</span> set the modal window options <span style="color:#006600; font-weight:bold;">*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jQuery<span style="color:#006600; font-weight:bold;">&#40;</span>document<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">ready</span><span style="color:#006600; font-weight:bold;">&#40;</span>function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'#modalFacebookEmail'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">hide</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">/*</span> Check out the FancyBox site <span style="color:#9966CC; font-weight:bold;">for</span> more information on the options <span style="color:#006600; font-weight:bold;">*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a.modalGroup&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">fancybox</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">'zoomSpeedIn'</span>: <span style="color:#006666;">200</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">'zoomSpeedOut'</span>: <span style="color:#006666;">250</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">'overlayShow'</span>: <span style="color:#0000FF; font-weight:bold;">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">'overlayOpacity'</span>: <span style="color:#006666;">0.6</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">'enableEscapeButton'</span>: <span style="color:#0000FF; font-weight:bold;">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">'hideOnContentClick'</span>: <span style="color:#0000FF; font-weight:bold;">false</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">'hideOnOverlayClick'</span>: <span style="color:#0000FF; font-weight:bold;">false</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#996600;">'centerOnScroll'</span>: <span style="color:#0000FF; font-weight:bold;">true</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">/*</span> Kickoff the modal window as soon as possible <span style="color:#006600; font-weight:bold;">*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; globalInterval = setInterval<span style="color:#006600; font-weight:bold;">&#40;</span>modalLoadFunction, <span style="color:#006666;">50</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;/</span>script<span style="color:#006600; font-weight:bold;">&gt;</span><br />
<br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Invisible link for the JS to click()</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>a id=<span style="color:#996600;">&quot;inline&quot;</span> href=<span style="color:#996600;">&quot;#modalFacebookEmail&quot;</span> <span style="color:#9966CC; font-weight:bold;">class</span>=<span style="color:#996600;">&quot;modalGroup&quot;</span> id=<span style="color:#996600;">&quot;modalFunction&quot;</span><span style="color:#006600; font-weight:bold;">&gt;&lt;/</span>a<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>div style=<span style="color:#996600;">&quot;display:none&quot;</span> id=<span style="color:#996600;">'modalFacebookEmail'</span><span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Change the value in the cookie so the modal window is only shown once</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> cookies<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:facebook_email_modal_shown</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#5A0A0A; font-weight:bold;">form_for</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>h3<span style="color:#006600; font-weight:bold;">&gt;</span>Facebook Email Address<span style="color:#006600; font-weight:bold;">&lt;/</span>h3<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>p<span style="color:#006600; font-weight:bold;">&gt;</span>If you want to login into the website <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#9966CC; font-weight:bold;">not</span> logged into Facebook, please provide your email address <span style="color:#9966CC; font-weight:bold;">and</span> a password.<span style="color:#006600; font-weight:bold;">&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>br <span style="color:#006600; font-weight:bold;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>p<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>strong<span style="color:#006600; font-weight:bold;">&gt;</span>Email Address<span style="color:#006600; font-weight:bold;">&lt;/</span>strong<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>br <span style="color:#006600; font-weight:bold;">/&gt;&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">%&gt;&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>p<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>strong<span style="color:#006600; font-weight:bold;">&gt;</span>Password<span style="color:#006600; font-weight:bold;">&lt;/</span>strong<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>br <span style="color:#006600; font-weight:bold;">/&gt;&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= f.<span style="color:#5A0A0A; font-weight:bold;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">%&gt;&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>p<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>strong<span style="color:#006600; font-weight:bold;">&gt;</span>Password Confirmation<span style="color:#006600; font-weight:bold;">&lt;/</span>strong<span style="color:#006600; font-weight:bold;">&gt;&lt;</span>br <span style="color:#006600; font-weight:bold;">/&gt;&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= f.<span style="color:#5A0A0A; font-weight:bold;">password_field</span> <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">%&gt;&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>p<span style="color:#006600; font-weight:bold;">&gt;&amp;</span>nbsp;<span style="color:#006600; font-weight:bold;">&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Note the method for closing the modal window</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span>p<span style="color:#006600; font-weight:bold;">&gt;&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">'Update'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> <span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&lt;</span>a onClick=<span style="color:#996600;">&quot;jQuery('a.modalGroup').fancybox.close()&quot;</span> href=<span style="color:#996600;">&quot;#&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span>Cancel<span style="color:#006600; font-weight:bold;">&lt;/</span>a<span style="color:#006600; font-weight:bold;">&gt;&lt;/</span>p<span style="color:#006600; font-weight:bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&lt;/</span>div<span style="color:#006600; font-weight:bold;">&gt;</span><br />
<span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></div></div>
<p>Now you have a modal window that is only shown once.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/ruby/rails/testing-sessions-with-digestsha256-passwords-in-rails/' rel='bookmark' title='Permanent Link: Testing Sessions with Digest::SHA256 Passwords In Rails'>Testing Sessions with Digest::SHA256 Passwords In Rails</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-1-of-2/' rel='bookmark' title='Permanent Link: Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)'>Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/country-state-select-using-carmen-and-jquery/' rel='bookmark' title='Permanent Link: Country-State Select Using Carmen and jQuery'>Country-State Select Using Carmen and jQuery</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/ruby/rails/one-time-modal-windows-with-rails-and-fancybox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Migrations Without belongs_to Or references</title>
		<link>http://eric.lubow.org/2009/ruby/rails/migrations-without-belongs_to-or-references/</link>
		<comments>http://eric.lubow.org/2009/ruby/rails/migrations-without-belongs_to-or-references/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 14:00:00 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=308</guid>
		<description><![CDATA[Normally when do a database migration in Rails, when adding ownership from a model to another model, you use the concept of belongs_to or references: &#160; create_table :comments do &#124;t&#124; &#160; &#160; t.belongs_to :user &#160; &#160; t.references :post &#160; end Interestingly enough, these methods are only available during the initial table creation. If you want [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fmigrations-without-belongs_to-or-references%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fmigrations-without-belongs_to-or-references%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Normally when do a database migration in Rails, when adding ownership from a model to another model, you use the concept of belongs_to or references:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; create_table <span style="color:#ff3333; font-weight:bold;">:comments</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; &nbsp; t.<span style="color:#9900CC;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:user</span><br />
&nbsp; &nbsp; t.<span style="color:#9900CC;">references</span> <span style="color:#ff3333; font-weight:bold;">:post</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Interestingly enough, these methods are only available during the initial table creation.  If you want to add a reference to a model that is created later, you have to do it the old fashioned way, by just adding a column:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;add_column <span style="color:#ff3333; font-weight:bold;">:comments</span>, <span style="color:#ff3333; font-weight:bold;">:group_id</span>, :<span style="color:#CC0066; font-weight:bold;">integer</span></div></div>
<p>Doing it this way is clean, easy, and definitely meets the KISS principle.  But I do find it interesting that one can&#8217;t add an association later in the game.  Sometimes the Rails way is just KISS and adding the column by hand.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-1-of-2/' rel='bookmark' title='Permanent Link: Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)'>Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/ruby/rails/migrations-without-belongs_to-or-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing zlib Errors On Capistrano Deploy</title>
		<link>http://eric.lubow.org/2009/ruby/rails/fixing-zlib-errors-on-capistrano-deploy/</link>
		<comments>http://eric.lubow.org/2009/ruby/rails/fixing-zlib-errors-on-capistrano-deploy/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 18:15:47 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[capistrano]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=272</guid>
		<description><![CDATA[Ever since I started doing Capistrano deploys from my Mac, I have been seeing the following error: zlib(finalizer): the stream was freed prematurely. The error seems harmless, but I figure that errors are there for a reason and the Sysadmin in me decided to try to get rid of it. A quick Google for an [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Ffixing-zlib-errors-on-capistrano-deploy%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Ffixing-zlib-errors-on-capistrano-deploy%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Ever since I started doing <a href="http://www.capify.org/">Capistrano</a> deploys from my Mac, I have been seeing the following error:</p>
<blockquote><p>zlib(finalizer): the stream was freed prematurely.</p></blockquote>
<p>The error seems harmless, but I figure that errors are there for a reason and the Sysadmin in me decided to try to get rid of it. A quick Google for an answer said something about adding the following line of code to you <strong>Capfile</strong> (or in my case, I separated it out to my <strong>config/deploy.rb</strong>:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ssh_options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:compression</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;none&quot;</span></div></div>
<p>I couldn&#8217;t actually find the reason for this.  But I guess finding the solution is good enough for now.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2007/perl/emailfind/' rel='bookmark' title='Permanent Link: Email::Find'>Email::Find</a></li>
<li><a href='http://eric.lubow.org/2009/mail/what-is-a-drd/' rel='bookmark' title='Permanent Link: What is a DRD'>What is a DRD</a></li>
<li><a href='http://eric.lubow.org/2010/mac/capistrano-hangs-on-mac-os-x-leopard/' rel='bookmark' title='Permanent Link: Capistrano Hangs on Mac OS X Leopard'>Capistrano Hangs on Mac OS X Leopard</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/ruby/rails/fixing-zlib-errors-on-capistrano-deploy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Shortcut Notation in Rails Callbacks</title>
		<link>http://eric.lubow.org/2009/ruby/rails/shortcut-notation-in-rails-callbacks/</link>
		<comments>http://eric.lubow.org/2009/ruby/rails/shortcut-notation-in-rails-callbacks/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 13:00:51 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=260</guid>
		<description><![CDATA[As I have spent the last few weeks/months learning Ruby and Rails, I have grown to love it more and more. The ease of use and the high learning curve make is fantastic for people like me who aren&#8217;t developers by nature. I have recently come across a scenario in Rails that there is not [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fshortcut-notation-in-rails-callbacks%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Feric.lubow.org%2F2009%2Fruby%2Frails%2Fshortcut-notation-in-rails-callbacks%2F&amp;source=elubow&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>As I have spent the last few weeks/months learning Ruby and Rails, I have grown to love it more and more.  The ease of use and the high learning curve make is fantastic for people like me who aren&#8217;t developers by nature.</p>
<p>I have recently come across a scenario in Rails that there is not yet a shortcut for.  If you have a method that you want to execute in a few different callbacks, there is no shortcut for it.  ie, I have the following:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; after_create &nbsp; <span style="color:#ff3333; font-weight:bold;">:methodFoo</span><br />
&nbsp; after_save &nbsp; &nbsp; <span style="color:#ff3333; font-weight:bold;">:methodFoo</span><br />
&nbsp; after_destroy &nbsp;<span style="color:#ff3333; font-weight:bold;">:methodFoo</span></div></div>
<p>I would like to be able to write this as:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;after_create, after_save, after_destroy <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:methodFoo</span></div></div>
<p>This would obviously only work if the callbacks were then treated as arrays of functions and each time a callback was found in the model, the method was pushed onto the array.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/ruby/rails/adding-an-average-column-to-a-model/' rel='bookmark' title='Permanent Link: Adding An Average Column To A Model'>Adding An Average Column To A Model</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/testing-sessions-with-digestsha256-passwords-in-rails/' rel='bookmark' title='Permanent Link: Testing Sessions with Digest::SHA256 Passwords In Rails'>Testing Sessions with Digest::SHA256 Passwords In Rails</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/rails/adding-ajax-bookmarks-to-your-rails-application-part-1-of-2/' rel='bookmark' title='Permanent Link: Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)'>Adding AJAX Bookmarks to Your Rails Application (Part 1 of 2)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/ruby/rails/shortcut-notation-in-rails-callbacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
