<?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>redirect_to :hack &#187; Rails</title>
	<atom:link href="http://blog.coderubik.com/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.coderubik.com</link>
	<description>Thoughts on Ruby on Rails and Facebook development</description>
	<lastBuildDate>Thu, 07 Apr 2011 21:49:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Cookies and Facebook canvas apps</title>
		<link>http://blog.coderubik.com/2011/03/cookies-and-facebook-canvas-apps/</link>
		<comments>http://blog.coderubik.com/2011/03/cookies-and-facebook-canvas-apps/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 22:38:26 +0000</pubDate>
		<dc:creator>Pierre Olivier Martel</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://blog.coderubik.com/?p=187</guid>
		<description><![CDATA[With the new Facebook way of loading iframes apps with POST requests, the safari third-party cookie problem is now partly fixed. It has always been a big headache dealing with cookies on the Facebook platform since Safari rejects them by default. With POST requests however, it is now possible to set cookies from the server. [...]]]></description>
			<content:encoded><![CDATA[<p>With the new Facebook way of loading iframes apps with <a href="http://developers.facebook.com/docs/canvas/post/">POST requests</a>, the safari third-party cookie problem is now partly fixed. It has always been a big headache dealing with cookies on the Facebook platform since Safari rejects them by default. With POST requests however, it is now possible to set cookies from the server. It seems Safari will still reject cookies set from the client.</p>
<p>So if you are using the Facebook Javascript SDK and set the cookies option to true, Safari won&#8217;t accept the FBS_XXXX cookie planted by the SDK. I struggled a whole week-end before <a href="http://forum.developers.facebook.net/viewtopic.php?pid=321880">figuring it out</a>.</p>
<p>The work-around I use is to set a cookie from the server with the signed_request in it if there is one present. This solves most of the problems since I can just use the cookie when I don&#8217;t have a fresh signed_request (this happens when I do a request inside the canvas).</p>
<p>Here is the code inside a before_filter in the application_controller. I use Rails with the <a href="https://github.com/mmangino/facebooker2">Facebooker2</a> gem :</p>
<p><script src="https://gist.github.com/861227.js?file=facebook_cookie.rb"></script></p>
<p>The P3P header is used to make IE6 and IE7 accept the cookie as well.</p>
<p>If you&#8217;re building a Facebook canvas application with Rails, take the time to also read <a title="Restful Facebook canvas app with Rails and POST For Canvas" href="http://blog.coderubik.com/2011/03/restful-facebook-canvas-app-with-rails-and-post-for-canvas/">my post about keeping your REST architecture with the POST requests</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.coderubik.com/2011/03/cookies-and-facebook-canvas-apps/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Rails geopositioning with Google Maps</title>
		<link>http://blog.coderubik.com/2009/10/rails-geopositioning-with-google-maps/</link>
		<comments>http://blog.coderubik.com/2009/10/rails-geopositioning-with-google-maps/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 14:57:37 +0000</pubDate>
		<dc:creator>Pierre Olivier Martel</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://modelix.net/?p=128</guid>
		<description><![CDATA[A common problem when building a Rails web application that deals with addresses is geopositioning. Geopositioning can have different meaning depending on what you’re trying to achieve. In this article and the following, I will describe my solution to the following problems : The user enters an address and you need to validate it and [...]]]></description>
			<content:encoded><![CDATA[<p>A common problem when building a Rails web application that deals with addresses is geopositioning. Geopositioning can have different meaning depending on what you’re trying to achieve. In this article and the following, I will describe my solution to the following problems :</p>
<ol>
<li>The user enters an address and you need to validate it and <strong>geoposition it (latitude &amp; longitude)</strong> on a map.</li>
<li>You need to <strong>display the google map</strong> for that given address on a webpage.</li>
</ol>
<p>Today, I will show you how I did the first step using the <a href="http://ym4r.rubyforge.org/"><span class="caps">YM4R</span>/GM plugin</a>.</p>
<h2>1- Setup the plugin and get you Google Maps key</h2>
<p>First thing to do is to install the <a href="http://ym4r.rubyforge.org/"><span class="caps">YM4R</span>/GM plugin</a> plugin. There is also a gem but it seems to be a stripped-down version of the plugin. I couldn’t get it working so I would advise to use the plugin :</p>
<pre>script/plugin install svn://rubyforge.org/var/svn/ym4r/Plugins/GM/trunk/ym4r_gm</pre>
<p>The plugin creates a file in your config folder named <code>gmaps_api_key.xml</code>. You need to enter your Google Maps <span class="caps">API </span>keys for the different environments. You can <a href="http://code.google.com/apis/maps/signup.html">get the keys here</a>.</p>
<h2>2- Create the model</h2>
<p>Next thing to do is to create your <code>Geolocation</code> activerecord model. You need to at least have those four attributes :</p>
<pre>create_table "geolocations", :force =&gt; true do |t|
  t.string   "address"
  t.string   "city"
  t.float    "latitude"
  t.float    "longitude"
end</pre>
<p>You might also want to add the country and postal code to the mix. But in most case, Google Maps will be able to return you a location with just a street address and a city.</p>
<h2>3- Set the geolocation on the before_save</h2>
<p>When you create a new gelocation record, you want to geoposition it. Here’s my code in the <code>Geolocation.rb</code> model :</p>
<pre>before_save :set_geolocation

def set_geolocation
  if changes['address'] || changes['city'] || !mapped?
    results = Geocoding::get(complete_address)
    if results.status == Geocoding::GEO_SUCCESS
      self.latitude, self.longitude = results[0].latlon
    else
      self.latitude, self.longitude = nil
    end
  end
end

def complete_address
  [address, city].select(&amp;:present?).join(', ')
end

def mapped?
  latitude &amp;&amp; longitude
end</pre>
<p>What this does is set the latitude and longitude for the given address on the before_save callback. We first check if the address or the city changed and if so we try to get a geocoding. If the <span class="caps">API </span>responds with <span class="caps">SUCCESS, </span>we set the latitude and longitude with the first result. If the <span class="caps">API </span>failed to return a valid address, we set the latitude and longitude to <code>nil</code>. We can check if the address was mapped using the <code>mapped?</code> method.</p>
<h2>4- Wrap it up</h2>
<p>Geopositionning  the address is only the first part of the problem. Next week I’ll show you how to use the latitude and longitude information we stored to display Google maps in your webpages.</p>
<p><a href="/2009/10/display-a-google-map-in-rails/"><strong>Next week : How to display a google map for a geopositioned address</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.coderubik.com/2009/10/rails-geopositioning-with-google-maps/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Speeding up Tunnlr</title>
		<link>http://blog.coderubik.com/2009/09/speeding-up-tunnlr/</link>
		<comments>http://blog.coderubik.com/2009/09/speeding-up-tunnlr/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 20:24:21 +0000</pubDate>
		<dc:creator>Pierre Olivier Martel</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://modelix.net/?p=51</guid>
		<description><![CDATA[When developing a Facebook or Facebook Connect application, there are times when Facebook servers will need to directly make requests to your application. This means your local web server has to be remotely accessible for incoming requests. Typically, when working locally, your mongrel or webrick instance running on port 3000 is not accessible unless you [...]]]></description>
			<content:encoded><![CDATA[<p>When developing a Facebook or Facebook Connect application,  there are times when Facebook servers will need to directly make requests to your application. This means your local web server has to be remotely accessible for incoming requests.</p>
<p>Typically, when working locally, your mongrel or webrick instance running on port 3000 is not accessible unless you enable port forwarding on your router. And even then, your <span class="caps">ISP </span>probably doesn’t assign you a fixed IP address which means you have to run a dynamic IP address service like <a href="http://www.dyndns.com">dyndns</a>. This quickly gets complicated and if you work behind a corporate network or develop from different locations (ex.: home, office, Starbucks…), this solution won’t even work.</p>
<p>Meet <a href="http://www.tunnlr.com">Tunnlr</a>, an easy to use service that makes your local Rails server accessible from the Internet by just typing a single rake command in your console. When you register to the service, you will be assigned a fixed <span class="caps">URL </span>address with a port number and this is the address you will use to configure your Facebook application and access it locally.</p>
<p>One downside to that approach is that since each request has to do a return trip to the Tunnlr servers, you loose the lightning speed loading time you’re used to when developing from your local machine (no network latency). I found a little hack that will bypass the need to do a return trip and it can be done in three easy steps</p>
<p><strong>1- Modify your <code>/etc/hosts</code> file and add the following line :</strong></p>
<pre class="brush: plain; title: ; notranslate">127.0.0.1	web1.tunnlr.com</pre>
<p>This tells your machine to route all requests made to the <code>web1.tunnlr.com</code> address to your local machine. Unfortunately, there is no way to specify port redirections in the hosts file so we need step 2 and 3 to make sure our remote and local ports are the same.</p>
<p><strong>2- Modify your tunnlr.yml or facebooker.yml file to make your local port the same as your remote port. In my case, this is port 10524.</strong></p>
<pre class="brush: plain; title: ; notranslate">
remote_port: 10524
local_port: 10524
</pre>
<p><strong>3- Start your Rails server with the same port that has been assigned to you by Tunnlr.</strong><br />
[/code]script/server -p 10524[/code]</p>
<p>That’s it! Now when you query <code>web1.tunnlr.com</code> your local requests will stay local and your server will still be remotely accessible from the tubes!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.coderubik.com/2009/09/speeding-up-tunnlr/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>redirect_to :back</title>
		<link>http://blog.coderubik.com/2008/11/rails-redirect_to-back/</link>
		<comments>http://blog.coderubik.com/2008/11/rails-redirect_to-back/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 19:07:00 +0000</pubDate>
		<dc:creator>Pierre Olivier Martel</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://modelix.net/?p=10</guid>
		<description><![CDATA[Here&#8217;s a useful trick I didn&#8217;t know about! The command redirect_to :back does just what it says, it redirects the user back to the previous action. This is similar to redirect_to(request.env["HTTP_REFERER"]). The Rails documentation states that this can be useful for forms that are triggered from multiple places. In my case I needed it to [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a useful trick I didn&#8217;t know about! The command <code>redirect_to :back</code> does just what it says, it redirects the user back to the previous action. This is similar to <code>redirect_to(request.env["HTTP_REFERER"])</code>.</p>
<p><a href="http://api.rubyonrails.org/classes/ActionController/Base.html#M000478">The Rails documentation</a> states that this can be useful for forms that are triggered from multiple places.</p>
<p>In my case I needed it to redisplay the same page after a post that didn&#8217;t validate. I could redirect the user back to the right action but some request parameters were missing in the post action that prevented this to work right.</p>
<p>For example, if the last request was <code>/my_action?location=there</code>, I would lose the <code>location</code> parameter if I didn&#8217;t send it along in the post. And I didn&#8217;t want to send the parameter along since I was calling a different controller which business logic didn&#8217;t relate to these parameters.</p>
<p>Well, by using the HTTP referer, Rails redirects back to the URL with the right parameters and sends the user to the exact same location he was. Neat!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.coderubik.com/2008/11/rails-redirect_to-back/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
