<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Rails Wizard</title>
	
	<link>http://www.railswizard.com</link>
	<description>Ruby on Rails tutorials, solutions and other software tips</description>
	<lastBuildDate>Wed, 07 Dec 2011 04:59:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RailsWizard" /><feedburner:info uri="railswizard" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Rails Solr Sunspot Geospatial Searches</title>
		<link>http://feedproxy.google.com/~r/RailsWizard/~3/uGxCAyhJDeQ/</link>
		<comments>http://www.railswizard.com/2011/12/06/rails-solr-sunspot-geospatial-searches/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 04:42:11 +0000</pubDate>
		<dc:creator>allen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.railswizard.com/?p=143</guid>
		<description><![CDATA[Using Gem &#8220;sunspot_solr&#8221; 1.3.0 I have &#8220;lat&#8221; and &#8220;lng&#8221; attributes associated with my model. I defined searchable as: model.rb searchable do float :lat, :as =&#62; 'lat' float :lng, :as =&#62; 'lng' end controller.rb def search search = Model.search do # 25 miles hard coded for now search_radius = 25 adjust_solr_params do &#124;params&#124; params&#91;:spatial&#93;=&#34;{!radius=#{search_radius} sort=true}lat:#{current_user.lat},lng:#{current_user.lng}&#34; end [...]]]></description>
			<content:encoded><![CDATA[<p>Using Gem &#8220;sunspot_solr&#8221; 1.3.0 </p>
<p>I have &#8220;lat&#8221; and &#8220;lng&#8221; attributes associated with my model. I defined searchable as:</p>
<p>model.rb</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="rails"><pre class="de1">searchable <span class="kw1">do</span>
  float <span class="re3">:lat</span>, <span class="re3">:as</span> <span class="sy0">=&gt;</span> <span class="st0">'lat'</span>
  float <span class="re3">:lng</span>, <span class="re3">:as</span> <span class="sy0">=&gt;</span> <span class="st0">'lng'</span>
<span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>controller.rb</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="rails"><pre class="de1"><span class="kw1">def</span> search 
  search = Model.<span class="me1">search</span> <span class="kw1">do</span>
     <span class="co1"># 25 miles hard coded for now                                                                       </span>
     search_radius = <span class="nu0">25</span>
     adjust_solr_params <span class="kw1">do</span> <span class="sy0">|</span>params<span class="sy0">|</span>
       params<span class="br0">&#91;</span><span class="re3">:spatial</span><span class="br0">&#93;</span>=<span class="st0">&quot;{!radius=#{search_radius} sort=true}lat:#{current_user.lat},lng:#{current_user.lng}&quot;</span>
     <span class="kw1">end</span>
  <span class="kw1">end</span>
  search.<span class="me1">results</span>
<span class="kw1">end</span></pre></div></div></div></div></div></div></div>



<p><a href="http://feedads.g.doubleclick.net/~a/WSlGZ6R6iuCAB1MDRrXA7j2JXX4/0/da"><img src="http://feedads.g.doubleclick.net/~a/WSlGZ6R6iuCAB1MDRrXA7j2JXX4/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/WSlGZ6R6iuCAB1MDRrXA7j2JXX4/1/da"><img src="http://feedads.g.doubleclick.net/~a/WSlGZ6R6iuCAB1MDRrXA7j2JXX4/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/RailsWizard/~4/uGxCAyhJDeQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.railswizard.com/2011/12/06/rails-solr-sunspot-geospatial-searches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.railswizard.com/2011/12/06/rails-solr-sunspot-geospatial-searches/</feedburner:origLink></item>
		<item>
		<title>ActiveRecord callbacks and destructive methods</title>
		<link>http://feedproxy.google.com/~r/RailsWizard/~3/QZQSLhp9uPc/</link>
		<comments>http://www.railswizard.com/2011/02/12/activerecord-callbacks-and-destructive-methods/#comments</comments>
		<pubDate>Sat, 12 Feb 2011 22:23:10 +0000</pubDate>
		<dc:creator>allen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.railswizard.com/?p=126</guid>
		<description><![CDATA[Recently I added search functionality to my application. I implemented a search controller and model. An issue I faced was manipulating the search keywords entered by the user so the search query worked properly. I ended up doing the following: search.rb class Search &#60; ActiveRecord::Base belongs_to :user attr_accessible :keywords, :category, :user &#160; validates_presence_of :keywords &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I added search functionality to my application. I implemented a search controller and model. An issue I faced was manipulating the search keywords entered by the user so the search query worked properly. I ended up doing the following:</p>
<p>search.rb</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="rails"><pre class="de1"><span class="kw1">class</span> Search <span class="sy0">&lt;</span> <span class="kw4"><span class="re2">ActiveRecord::Base</span></span>
  <span class="kw5">belongs_to</span> <span class="re3">:user</span>
  attr_accessible <span class="re3">:keywords</span>, <span class="re3">:category</span>, <span class="re3">:user</span>
&nbsp;
  <span class="kw5">validates_presence_of</span> <span class="re3">:keywords</span>
&nbsp;
 <span class="co1"># DON'T CHAIN DESTRUCTIVE METHODS!</span>
  <span class="kw5">before_save</span> <span class="kw4">Proc</span>.<span class="kw5">new</span><span class="br0">&#123;</span><span class="sy0">|</span>search<span class="sy0">|</span> search.<span class="me1">keywords</span>.<span class="me1">strip</span>!.<span class="me1">downcase</span>!<span class="br0">&#125;</span>
&nbsp;
  <span class="co1">#....</span>
<span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>I got some really weird behavior when the before_save callback fired and later learned not to chain destructive methods!<br />
<a href="http://buddingrubyist.com/2007/12/06/dont-blindly-chain-destructive-array-methods/">See this for more information</a>.</p>
<p>So the solution was:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="rails"><pre class="de1"><span class="kw5">before_save</span> <span class="kw4">Proc</span>.<span class="kw5">new</span><span class="br0">&#123;</span><span class="sy0">|</span>search<span class="sy0">|</span> search.<span class="me1">keywords</span> = search.<span class="me1">keywords</span>.<span class="me1">strip</span>.<span class="me1">downcase</span><span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>Alternatively I could implement a method to trim and downcase the keywords also:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="rails"><pre class="de1"><span class="kw5">before_save</span> <span class="re3">:clean_keywords</span>
&nbsp;
<span class="kw1">def</span> clean_keywords
   keywords = keywords.<span class="me1">strip</span>.<span class="me1">downcase</span>
<span class="kw1">end</span></pre></div></div></div></div></div></div></div>



<p><a href="http://feedads.g.doubleclick.net/~a/Haj3u7UXRrfJ9BCfAcoJPbe-oqs/0/da"><img src="http://feedads.g.doubleclick.net/~a/Haj3u7UXRrfJ9BCfAcoJPbe-oqs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Haj3u7UXRrfJ9BCfAcoJPbe-oqs/1/da"><img src="http://feedads.g.doubleclick.net/~a/Haj3u7UXRrfJ9BCfAcoJPbe-oqs/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/RailsWizard/~4/QZQSLhp9uPc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.railswizard.com/2011/02/12/activerecord-callbacks-and-destructive-methods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.railswizard.com/2011/02/12/activerecord-callbacks-and-destructive-methods/</feedburner:origLink></item>
		<item>
		<title>Ruby on Rails 3 RESTful and default routes</title>
		<link>http://feedproxy.google.com/~r/RailsWizard/~3/XDLW3AfAp28/</link>
		<comments>http://www.railswizard.com/2011/01/22/ruby-on-rails-3-restful-and-default-routes/#comments</comments>
		<pubDate>Sat, 22 Jan 2011 22:47:44 +0000</pubDate>
		<dc:creator>allen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.railswizard.com/?p=114</guid>
		<description><![CDATA[You have probably created some action in your controller class and can&#8217;t access it right? Well you need to have some default routing rules set up in order for rails to understand the url and route it to the appropriate action and controller. This will happen alot with AJAX calls where you simply want to [...]]]></description>
			<content:encoded><![CDATA[<p>You have probably created some action in your controller class and can&#8217;t access it right? Well you need to have some default routing rules set up in order for rails to understand the url and route it to the appropriate action and controller. This will happen alot with AJAX calls where you simply want to go to an action with no ID. Or maybe you want to go to an action but supply a bunch of parameters without an ID.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1"><span class="kw1">class</span> HomeController <span class="sy0">&lt;</span> ApplicationController
&nbsp;
  <span class="kw1">def</span> dothisaction
   <span class="co1"># do some stuff</span>
  <span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>Assuming you have defined the home controller as a RESTful controller by adding to your routes.rb</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1">resources <span class="re3">:home</span></pre></div></div></div></div></div></div></div>


<p>In your webbrowser:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="html4strict"><pre class="de1">localhost/home/dothisaction</pre></div></div></div></div></div></div></div>


<p>You get:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="text"><pre class="de1">Started GET &quot;/home/foo&quot;
&nbsp;
AbstractController::ActionNotFound (The action 'show' could not be found for HomeController):</pre></div></div></div></div></div></div></div>


<p>What is happening? Rails is matching the RESTful rule:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="text"><pre class="de1">home GET    /home/:id(.:format)                    {:controller=&gt;&quot;home&quot;, :action=&gt;&quot;show&quot;}</pre></div></div></div></div></div></div></div>


<p>to this URL request. We obviously don&#8217;t want that.</p>
<p>So if you want this controller to be RESTful, you must manually add each non RESTful route to your routes.rb like so:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="rails"><pre class="de1">match <span class="st0">'home/dothisaction'</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;home#dothisaction&quot;</span>  <span class="co1">#Must be before 'resources :home' line to take precedence </span>
resources <span class="re3">:home</span></pre></div></div></div></div></div></div></div>


<p>Or you can remove the &#8220;resources :home&#8221; line in the routes.rb and you can add (or uncomment)</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="rails"><pre class="de1">match <span class="st0">':controller(/:action(/:id(.:format)))'</span></pre></div></div></div></div></div></div></div>


<p>This line will automap any action method you have defined in a controller to a url. The parenthesis indicate that the argument is optional. So the url:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="text"><pre class="de1">localhost/home/dothisaction/5.pdf
&nbsp;
will map the parameters:
&nbsp;
:action =&gt; &quot;dothisaction&quot;
:id =&gt; 5
:format =&gt; &quot;pdf&quot;</pre></div></div></div></div></div></div></div>


<p>Leaving off the :id and :format parameters will work just as well.</p>
<p>Be aware that routing precedence is defined by the order the rules appear in routes.rb. </p>

<p><a href="http://feedads.g.doubleclick.net/~a/b7n7tMAuvPTmk5JEqb-9obmb4Ww/0/da"><img src="http://feedads.g.doubleclick.net/~a/b7n7tMAuvPTmk5JEqb-9obmb4Ww/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/b7n7tMAuvPTmk5JEqb-9obmb4Ww/1/da"><img src="http://feedads.g.doubleclick.net/~a/b7n7tMAuvPTmk5JEqb-9obmb4Ww/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/RailsWizard/~4/XDLW3AfAp28" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.railswizard.com/2011/01/22/ruby-on-rails-3-restful-and-default-routes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.railswizard.com/2011/01/22/ruby-on-rails-3-restful-and-default-routes/</feedburner:origLink></item>
		<item>
		<title>Example using ruby blocks, lambda and Proc</title>
		<link>http://feedproxy.google.com/~r/RailsWizard/~3/biqvBHPa6lU/</link>
		<comments>http://www.railswizard.com/2011/01/08/example-using-ruby-blocks-lambda-and-proc/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 19:07:50 +0000</pubDate>
		<dc:creator>allen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ruby 1.9.2]]></category>
		<category><![CDATA[ruby blocks]]></category>
		<category><![CDATA[ruby lambda]]></category>
		<category><![CDATA[ruby Proc]]></category>
		<category><![CDATA[ruby self]]></category>

		<guid isPermaLink="false">http://www.railswizard.com/?p=92</guid>
		<description><![CDATA[What really distinguishes ruby from other languages is the syntatic sugar of blocks. For example, blocks really come in handy when iterating thru a collection. The example: &#40;1..10&#41;.collect&#123;&#124;i&#124; i*i&#125; =&#62; &#91;1, 4, 9, 16, 25, 36, 49, 64, 81, 100&#93; But how does the collect method work? Let&#8217;s define an array containing all the prime [...]]]></description>
			<content:encoded><![CDATA[<p>What really distinguishes ruby from other languages is the syntatic sugar of blocks. For example, blocks really come in handy when iterating thru a collection.</p>
<p>The example:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1"><span class="br0">&#40;</span><span class="nu0">1</span>..<span class="nu0">10</span><span class="br0">&#41;</span>.<span class="me1">collect</span><span class="br0">&#123;</span><span class="sy0">|</span>i<span class="sy0">|</span> i<span class="sy0">*</span>i<span class="br0">&#125;</span>
<span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="nu0">1</span>, <span class="nu0">4</span>, <span class="nu0">9</span>, <span class="nu0">16</span>, <span class="nu0">25</span>, <span class="nu0">36</span>, <span class="nu0">49</span>, <span class="nu0">64</span>, <span class="nu0">81</span>, <span class="nu0">100</span><span class="br0">&#93;</span></pre></div></div></div></div></div></div></div>


<p>But how does the collect method work?</p>
<p>Let&#8217;s define an array containing all the prime number up to 100.<br />
Note you will need to be running ruby 1.9.2 to access the Prime class:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1"><span class="kw3">require</span> <span class="st0">&quot;prime&quot;</span>
prime_numbers = <span class="br0">&#91;</span><span class="br0">&#93;</span>
Prime.<span class="me1">each</span><span class="br0">&#40;</span><span class="nu0">100</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="sy0">|</span>prime<span class="sy0">|</span> prime_numbers <span class="sy0">&lt;&lt;</span> prime <span class="br0">&#125;</span>
 <span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">5</span>, <span class="nu0">7</span>, <span class="nu0">11</span>, <span class="nu0">13</span>, <span class="nu0">17</span>, <span class="nu0">19</span>, <span class="nu0">23</span>, <span class="nu0">29</span>, <span class="nu0">31</span>, <span class="nu0">37</span>, <span class="nu0">41</span>, <span class="nu0">43</span>, <span class="nu0">47</span>, <span class="nu0">53</span>, <span class="nu0">59</span>, <span class="nu0">61</span>, <span class="nu0">67</span>, <span class="nu0">71</span>, <span class="nu0">73</span>, <span class="nu0">79</span>, <span class="nu0">83</span>, <span class="nu0">89</span>, <span class="nu0">97</span><span class="br0">&#93;</span></pre></div></div></div></div></div></div></div>


<p>Now let&#8217;s define our own &#8220;change!&#8221; method which takes a block, changing the value of each element in our prime_number array object according to the passed block. Note the &#8220;bang&#8221; or &#8220;!&#8221; is appended to a method when the behavior is destructive or object altering.</p>
<p>Define our custom method:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="rails"><pre class="de1"><span class="kw1">class</span> <span class="sy0">&lt;&lt;</span> prime_numbers
  <span class="kw1">def</span> change!<span class="br0">&#40;</span><span class="sy0">&amp;</span>b<span class="br0">&#41;</span>
    <span class="kw3">puts</span> <span class="st0">&quot;Calling change!&quot;</span>
    <span class="kw2">self</span>.<span class="me1">each_index</span> <span class="br0">&#123;</span> <span class="sy0">|</span>i<span class="sy0">|</span> <span class="kw2">self</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span> = b.<span class="kw5">call</span><span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span>
  <span class="kw1">end</span>
<span class="kw1">end</span>
&nbsp;
prime_numbers.<span class="me1">change</span>!<span class="br0">&#123;</span><span class="sy0">|</span><span class="kw3">p</span><span class="sy0">|</span> <span class="kw3">p</span> <span class="sy0">*</span> <span class="kw3">p</span><span class="br0">&#125;</span>
<span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="nu0">4</span>, <span class="nu0">9</span>, <span class="nu0">25</span>, <span class="nu0">49</span>, <span class="nu0">121</span>, <span class="nu0">169</span>, <span class="nu0">289</span>, <span class="nu0">361</span>, <span class="nu0">529</span>, <span class="nu0">841</span>, <span class="nu0">961</span>, <span class="nu0">1369</span>, <span class="nu0">1681</span>, <span class="nu0">1849</span>, <span class="nu0">2209</span>, <span class="nu0">2809</span>, <span class="nu0">3481</span>, <span class="nu0">3721</span>, <span class="nu0">4489</span>, <span class="nu0">5041</span>, <span class="nu0">5329</span>, <span class="nu0">6241</span>, <span class="nu0">6889</span>, <span class="nu0">7921</span>, <span class="nu0">9409</span><span class="br0">&#93;</span></pre></div></div></div></div></div></div></div>


<p>If we want to explicitly create a lambda or proc and pass that along instead:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1">square = <span class="kw3">lambda</span> <span class="br0">&#123;</span><span class="sy0">|</span>x<span class="sy0">|</span> x <span class="sy0">*</span> x<span class="br0">&#125;</span>
prime_numbers.<span class="me1">change</span>!<span class="br0">&#40;</span><span class="sy0">&amp;</span>square<span class="br0">&#41;</span>
<span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="nu0">4</span>, <span class="nu0">9</span>, <span class="nu0">25</span>, <span class="nu0">49</span>, <span class="nu0">121</span>, <span class="nu0">169</span>, <span class="nu0">289</span>, <span class="nu0">361</span>, <span class="nu0">529</span>, <span class="nu0">841</span>, <span class="nu0">961</span>, <span class="nu0">1369</span>, <span class="nu0">1681</span>, <span class="nu0">1849</span>, <span class="nu0">2209</span>, <span class="nu0">2809</span>, <span class="nu0">3481</span>, <span class="nu0">3721</span>, <span class="nu0">4489</span>, <span class="nu0">5041</span>, <span class="nu0">5329</span>, <span class="nu0">6241</span>, <span class="nu0">6889</span>, <span class="nu0">7921</span>, <span class="nu0">9409</span><span class="br0">&#93;</span></pre></div></div></div></div></div></div></div>


<p>The &#8220;&#038;&#8221; operator turns the lambda or Proc object into a block. When &#8220;&#038;&#8221; is used in the context of a method argument, it creates a Proc object from passed block so the method can now execute the &#8220;call&#8221; method on the in the block. The &#8220;call&#8221; method is a method in the &#8220;Proc&#8221; class.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/aWTnb1WvclYb1OA9oWY5iWQrGEU/0/da"><img src="http://feedads.g.doubleclick.net/~a/aWTnb1WvclYb1OA9oWY5iWQrGEU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/aWTnb1WvclYb1OA9oWY5iWQrGEU/1/da"><img src="http://feedads.g.doubleclick.net/~a/aWTnb1WvclYb1OA9oWY5iWQrGEU/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/RailsWizard/~4/biqvBHPa6lU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.railswizard.com/2011/01/08/example-using-ruby-blocks-lambda-and-proc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.railswizard.com/2011/01/08/example-using-ruby-blocks-lambda-and-proc/</feedburner:origLink></item>
		<item>
		<title>Ruby Metaprogramming Basics</title>
		<link>http://feedproxy.google.com/~r/RailsWizard/~3/tSfr36QLQNo/</link>
		<comments>http://www.railswizard.com/2011/01/03/ruby-metaprogramming-basics/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 03:35:33 +0000</pubDate>
		<dc:creator>allen</dc:creator>
				<category><![CDATA[Ruby on Rails Video Tutorials]]></category>
		<category><![CDATA[ruby 1.8.7]]></category>
		<category><![CDATA[ruby 1.9.2]]></category>
		<category><![CDATA[ruby class diagram]]></category>
		<category><![CDATA[ruby class methods]]></category>
		<category><![CDATA[ruby classes]]></category>
		<category><![CDATA[ruby instance methods]]></category>
		<category><![CDATA[ruby metaprogramming]]></category>
		<category><![CDATA[ruby objects]]></category>
		<category><![CDATA[ruby self]]></category>

		<guid isPermaLink="false">http://www.railswizard.com/?p=81</guid>
		<description><![CDATA[Understand Ruby Metaprogramming in this instructional video by learning how classes and object interact and what the &#8220;self&#8221; object is.]]></description>
			<content:encoded><![CDATA[<p>Understand Ruby Metaprogramming in this instructional video by learning how classes and object interact and what the &#8220;self&#8221; object is.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/_ukZ2lNfYtnK4mKeTREyAT6ou58/0/da"><img src="http://feedads.g.doubleclick.net/~a/_ukZ2lNfYtnK4mKeTREyAT6ou58/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/_ukZ2lNfYtnK4mKeTREyAT6ou58/1/da"><img src="http://feedads.g.doubleclick.net/~a/_ukZ2lNfYtnK4mKeTREyAT6ou58/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/RailsWizard/~4/tSfr36QLQNo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.railswizard.com/2011/01/03/ruby-metaprogramming-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.railswizard.com/2011/01/03/ruby-metaprogramming-basics/</feedburner:origLink></item>
		<item>
		<title>Dynamically update URL using jQuery</title>
		<link>http://feedproxy.google.com/~r/RailsWizard/~3/z9PFy44HyLg/</link>
		<comments>http://www.railswizard.com/2010/12/30/dynamically-changing-url-parameters-with-jquery/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 03:29:05 +0000</pubDate>
		<dc:creator>allen</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.railswizard.com/?p=44</guid>
		<description><![CDATA[I am using jQuery FullCalendar for a project and the view can be either a day, a month, or a year. The user can move around the calendar by clicking back and forward arrows, and none of these clicks will cause a page refresh. However the &#8220;start&#8221; and &#8220;end&#8221; dates change depending on the current [...]]]></description>
			<content:encoded><![CDATA[<p>I am using <a href="http://arshaw.com/fullcalendar/">jQuery FullCalendar</a> for a project and the view can be either a day, a month, or a year. The user can move around the calendar by clicking back and forward arrows, and none of these clicks will cause a page refresh. </p>
<p>However the &#8220;start&#8221; and &#8220;end&#8221; dates change depending on the current calendar view. I created a button element on the page to print the current calendar view, which in my case will be all the &#8220;events&#8221; that fall within the start and end date range.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="html4strict"><pre class="de1"> <span class="sc2">&lt;<span class="kw2">span</span> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;print-schedule&quot;</span>&gt;</span>Print<span class="sc2">&lt;<span class="sy0">/</span><span class="kw2">span</span>&gt;</span></pre></div></div></div></div></div></div></div>


<p>The &#8216;print-schedule&#8217; element has no click handler so I add one with some jQuery code:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="jquery"><pre class="de1"> <span class="sy1">$</span><span class="br0">&#40;</span><span class="st0">'span#print-schedule'</span><span class="br0">&#41;</span>.<span class="me1">button</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="kw9">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>                                        
      <span class="sy1">$</span>.<span class="kw11">get</span><span class="br0">&#40;</span><span class="st0">&quot;/events/printview&quot;</span><span class="sy0">,</span>                                                                               
            <span class="br0">&#123;</span> startdate<span class="sy0">:</span> <span class="sy1">$</span>.<span class="me1">fullCalendar</span>.<span class="me1">formatDate</span><span class="br0">&#40;</span>view.<span class="me1">start</span><span class="sy0">,</span> <span class="st0">&quot;dd-MM-yyyy&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span>                            
               enddate<span class="sy0">:</span> <span class="sy1">$</span>.<span class="me1">fullCalendar</span>.<span class="me1">formatDate</span><span class="br0">&#40;</span>view.<span class="kw6">end</span><span class="sy0">,</span> <span class="st0">&quot;dd-MM-yyyy&quot;</span><span class="br0">&#41;</span>                                 
             <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
      <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span>
 <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>But notice the problem here: $.get is an AJAX call to the action &#8220;printview&#8221; in my events controller. Ajax will not return anything to the browser visible to the user. For this to work we want a full page redirect to occur. In order to do this, I had to rewrite the jquery method as follows:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="jquery"><pre class="de1">  <span class="sy1">$</span><span class="br0">&#40;</span><span class="st0">'span#print-schedule'</span><span class="br0">&#41;</span>.<span class="me1">button</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="kw9">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> 
                url <span class="sy0">=</span> <span class="st0">&quot;/events/printview?startdate=&quot;</span> <span class="sy0">+</span> <span class="sy1">$</span>.<span class="me1">fullCalendar</span>.<span class="me1">formatDate</span><span class="br0">&#40;</span>view.<span class="me1">start</span><span class="sy0">,</span> <span class="st0">&quot;dd-MM-yyyy&quot;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;&amp;enddate=&quot;</span> <span class="sy0">+</span>
                        <span class="sy1">$</span>.<span class="me1">fullCalendar</span>.<span class="me1">formatDate</span><span class="br0">&#40;</span>view.<span class="kw6">end</span><span class="sy0">,</span> <span class="st0">&quot;dd-MM-yyyy&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                        window.<span class="me1">location</span>.<span class="me1">href</span> <span class="sy0">=</span> url<span class="sy0">;</span>                                                                     
     <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>Now the page redirects to the view page &#8220;printview&#8221; using the dynamically created url. </p>
<p>And the rails action code:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1"><span class="kw1">class</span> EventsController <span class="sy0">&lt;</span> ApplicationController 
...
  <span class="kw1">def</span> printview                                                                                               
      startdate = <span class="kw4">Date</span>.<span class="me1">parse</span><span class="br0">&#40;</span>params<span class="br0">&#91;</span><span class="re3">:startdate</span><span class="br0">&#93;</span><span class="br0">&#41;</span>                                                                    
      enddate = <span class="kw4">Date</span>.<span class="me1">parse</span><span class="br0">&#40;</span>params<span class="br0">&#91;</span><span class="re3">:enddate</span><span class="br0">&#93;</span><span class="br0">&#41;</span> 
      <span class="re1">@events</span> = Event.<span class="me1">find</span><span class="br0">&#40;</span><span class="re3">:all</span>, <span class="re3">:conditions</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="st0">&quot;DATE(starttime) &gt;= ? and DATE(endtime) &lt;= ? &quot;</span>, startdate, enddate <span class="br0">&#93;</span> <span class="br0">&#41;</span>           
   <span class="kw1">end</span> 
<span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>Here&#8217;s a screenshot of the calendar with the print button at the top<br />
<div id="attachment_62" class="wp-caption aligncenter" style="width: 500px"><a href="http://www.railswizard.com/wp-content/uploads/2010/12/calendar.gif"><img src="http://www.railswizard.com/wp-content/uploads/2010/12/calendar.gif" alt="calendar" title="calendar" width="500" height="361" class="size-full wp-image-62" /></a><div class="wp-caption-text">Jquery Full Calendar Screenshot</div></div></p>

<p><a href="http://feedads.g.doubleclick.net/~a/Nn4lbS-Dnw_FDc7Ke4h_bdDXs9w/0/da"><img src="http://feedads.g.doubleclick.net/~a/Nn4lbS-Dnw_FDc7Ke4h_bdDXs9w/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Nn4lbS-Dnw_FDc7Ke4h_bdDXs9w/1/da"><img src="http://feedads.g.doubleclick.net/~a/Nn4lbS-Dnw_FDc7Ke4h_bdDXs9w/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/RailsWizard/~4/z9PFy44HyLg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.railswizard.com/2010/12/30/dynamically-changing-url-parameters-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.railswizard.com/2010/12/30/dynamically-changing-url-parameters-with-jquery/</feedburner:origLink></item>
		<item>
		<title>How to install RVM with ruby on rails</title>
		<link>http://feedproxy.google.com/~r/RailsWizard/~3/rmqWu_oRdMk/</link>
		<comments>http://www.railswizard.com/2010/12/28/how-to-install-rvm-with-ruby-on-rails/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 03:40:00 +0000</pubDate>
		<dc:creator>allen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Ruby on Rails Video Tutorials]]></category>
		<category><![CDATA[rails 3]]></category>
		<category><![CDATA[ruby 1.8.7]]></category>
		<category><![CDATA[ruby 1.9.2]]></category>
		<category><![CDATA[ruby enterprise]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[RVM]]></category>

		<guid isPermaLink="false">http://www.railswizard.com/2010/12/28/how-to-install-rvm-with-ruby-on-rails/</guid>
		<description><![CDATA[Learn how to install and configure RVM with both rails 3 and rails 2 apps. RVM allows users to deploy each project with its own self-contained and dedicated environment&#8211;from the specific version of ruby all the way down to the precise set of required gems to run the application.]]></description>
			<content:encoded><![CDATA[<p>Learn how to install and configure RVM with both rails 3 and rails 2 apps. </p>
<p>RVM  allows users to deploy each project with its own self-contained and dedicated environment&#8211;from the specific version of<br />
ruby all the way down to the precise set of required gems to run the<br />
application.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/jZnZzkGL7JRlZdcyk7Ni1epwsD0/0/da"><img src="http://feedads.g.doubleclick.net/~a/jZnZzkGL7JRlZdcyk7Ni1epwsD0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/jZnZzkGL7JRlZdcyk7Ni1epwsD0/1/da"><img src="http://feedads.g.doubleclick.net/~a/jZnZzkGL7JRlZdcyk7Ni1epwsD0/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/RailsWizard/~4/rmqWu_oRdMk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.railswizard.com/2010/12/28/how-to-install-rvm-with-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.railswizard.com/2010/12/28/how-to-install-rvm-with-ruby-on-rails/</feedburner:origLink></item>
		<item>
		<title>Ruby to Javascript Dates</title>
		<link>http://feedproxy.google.com/~r/RailsWizard/~3/UNXk5A6cfgo/</link>
		<comments>http://www.railswizard.com/2010/12/18/javascript-to-ruby-dates/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 03:39:02 +0000</pubDate>
		<dc:creator>allen</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.railswizard.com/?p=4</guid>
		<description><![CDATA[Recently I was using the JQuery UI datepicker widget and needed to restrict dates to a range in the popup calendar selector. In order to do so I had to pass the Ruby Date to javascript. My goal is usually to write as little javascript/jquery code as possible, i.e. to do all the necessary conversions [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was using the JQuery UI datepicker widget and needed to restrict dates to a range in the popup calendar selector. In order to do so I had to pass the Ruby Date to javascript. My goal is usually to write as little javascript/jquery code as possible, i.e. to do all the necessary conversions on the ruby/rails server side.</p>
<p>Once I got the format correct it was surprisingly easily.</p>
<p>Ruby code:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1"><span class="re1">@min_date_str</span> = date1.<span class="me1">strftime</span><span class="br0">&#40;</span><span class="st0">&quot;%B %d, %Y 00:00:00&quot;</span><span class="br0">&#41;</span>
<span class="re1">@max_date_str</span> = date2.<span class="me1">strftime</span><span class="br0">&#40;</span><span class="st0">&quot;%B %d, %Y 00:00:00&quot;</span><span class="br0">&#41;</span></pre></div></div></div></div></div></div></div>


<p>JQuery / Javascript Code:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="jquery"><pre class="de1"><span class="kw4">jQuery</span><span class="br0">&#40;</span>document<span class="br0">&#41;</span>.<span class="kw9">ready</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="sy1">$</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="sy1">$</span><span class="br0">&#40;</span><span class="st0">&quot;#start_date&quot;</span><span class="br0">&#41;</span>.<span class="me1">datepicker</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
        minDate<span class="sy0">:</span> <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="st0">&quot;&lt;%= @min_date_str %&gt;&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        maxDate<span class="sy0">:</span> <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="st0">&quot;&lt;%= @max_date_str %&gt;&quot;</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="sy1">$</span><span class="br0">&#40;</span><span class="st0">&quot;#end_date&quot;</span><span class="br0">&#41;</span>.<span class="me1">datepicker</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
        minDate<span class="sy0">:</span> <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="st0">&quot;&lt;%= @min_date_str %&gt;&quot;</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        maxDate<span class="sy0">:</span> <span class="kw2">new</span> Date<span class="br0">&#40;</span><span class="st0">&quot;&lt;%= @max_date_str %&gt;&quot;</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>Note:  Javascript Date objects include a Time component (unlike the Ruby Date object). Ruby would need a DateTime object to include the time component.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/qdEMVdQJE31oeU8rYUfPcruBMDI/0/da"><img src="http://feedads.g.doubleclick.net/~a/qdEMVdQJE31oeU8rYUfPcruBMDI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/qdEMVdQJE31oeU8rYUfPcruBMDI/1/da"><img src="http://feedads.g.doubleclick.net/~a/qdEMVdQJE31oeU8rYUfPcruBMDI/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/RailsWizard/~4/UNXk5A6cfgo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.railswizard.com/2010/12/18/javascript-to-ruby-dates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.railswizard.com/2010/12/18/javascript-to-ruby-dates/</feedburner:origLink></item>
	</channel>
</rss>

