<?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; javascript</title>
	<atom:link href="http://eric.lubow.org/tag/javascript/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>Fri, 18 Nov 2011 14:56:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<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[<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='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='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='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[<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='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='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='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[<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><span style="color:#CC0066; font-weight:bold;">format</span><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='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='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='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[<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/country-state-select-using-carmen-and-jquery/' rel='bookmark' title='Country-State Select Using Carmen and jQuery'>Country-State Select Using Carmen and jQuery</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='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/adding-ajax-bookmarks-to-your-rails-application-part-2-of-2/' rel='bookmark' title='Adding AJAX Bookmarks to Your Rails Application (Part 2 of 2)'>Adding AJAX Bookmarks to Your Rails Application (Part 2 of 2)</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>
	</channel>
</rss>

