<?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>James Nachbar&#039;s Blog &#187; Web Programming</title>
	<atom:link href="http://www.nachbar.name/blog/category/programming/web-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nachbar.name/blog</link>
	<description>Programming and Plastic Surgery</description>
	<lastBuildDate>Thu, 19 Apr 2012 15:54:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Ruby on Rails 2.3 and PostgreSQL on Ubuntu Hardy 8.04 LTS and 10.04 LTS Server</title>
		<link>http://www.nachbar.name/blog/2008/11/28/rails-and-postgresql-on-ubuntu-hardy-804-lts/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rails-and-postgresql-on-ubuntu-hardy-804-lts</link>
		<comments>http://www.nachbar.name/blog/2008/11/28/rails-and-postgresql-on-ubuntu-hardy-804-lts/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 22:45:36 +0000</pubDate>
		<dc:creator>nachbar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Ruby on Rails Programming]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://nachbar.name/blog/?p=8</guid>
		<description><![CDATA[There has been much confusion and consternation about setting up Ruby on Rails with PostgreSQL.  Here are the detailed instructions for getting this to work on a fresh Ubuntu LTS 8.04 server, including a virtual server.]]></description>
			<content:encoded><![CDATA[<h3>Update: A few changes for 10.04 LTS, using PostgreSQL 8.4</h3>
<p>When running rails (other than rails &#8211;version), I got the error &#8220;No such file to load: net/https&#8221;. That was fixed by installing libopenssl-ruby, as in:<br />
aptitude install libopenssl-ruby<br />
This should probably be done before installing rails, although installing it after rails was installed fixed the problem</p>
<p>gem update &#8211;system produces a message that &#8220;gem update &#8211;system is disabled on Debian. RubyGems can be updated using the official Debian repositiries by aptitude or apt-get&#8221;</p>
<p>Instead, I found the following at https://help.ubuntu.com/community/RubyOnRails<br />
<code><br />
sudo gem install rubygems-update<br />
sudo update_rubygems note: this will clean out your gems!<br />
</code><br />
Note: I had to reinstall rails after update_rubygems, which I ran after I had installed rails. I would probably do this before installing rails.</p>
<p>The &#8220;-y&#8221; flag is now the default, and if you use it you get a message to that effect</p>
<p>irb and apache2 were already installed by the time I got to those steps. There is an apache2 metapackage that I would probably use instead of the apache 2.2 packages noted below if I still needed to install apache2.</p>
<p>Before you can run the programs you have installed with gem (e.g. Rails), you will need to add:<br />
<code>export PATH=/var/lib/gems/1.8/bin:$PATH</code></p>
<p>When I ran the Passenger installation, I got a message to install three more packages:<br />
<code><br />
aptitude install apache2-prefork-dev<br />
aptitude install libapr1-dev<br />
aptitude install libaprutil1-dev</code><br />
However, only the first one of those actually did anything. Since the passenger installation gives good diagnostics, it is reasonable to let that tell you what still needs to be installed.</p>
<p>Following the instructions on the Passenger install for configuring Apache, the sample configuration included some inline comments with &#8216;#&#8217; &#8212; these caused an error in Apache2 and had to be moved to a separate line.</p>
<p>Passenger may need a file named .htaccess to be installed in the /public directory of your rails app, with the following two lines:<br />
<code><br />
PassengerEnabled on<br />
PassengerAppRoot /full/path/to/the/root/of/your/rails/app</code><br />
The PassengerAppRoot should NOT be your rails app&#8217;s public directory, but the .htaccess file needs to be in that public directory. The Passenger docs incorrectly state that the PassengerAppRoot is assumed to be the parent of the public directory, but that is only true if the public directory is named in DocumentRoot, and not if you are using an alias.</p>
<p>Also, if you are using an alias and the Rails app is not in the root of the website, you may need <code>config.action_controller.relative_url_root = "/test"</code> in your config/environment.rb file</p>
<h3>Also note that, except where noted, the installation commands need to be run as root (<code>sudo su -</code>) or with sudo.</h3>
<h2>There has been much confusion and consternation about setting up Ruby on Rails with PostgreSQL</h2>
<p>(e.g., see: <a href="http://joshkim.org/2008/10/26/postgresql-ruby-and-rails-i-quit" target="_blank">http://joshkim.org/2008/10/26/postgresql-ruby-and-rails-i-quit</a>)</p>
<p>There seems to be a lot of support for running this on a Mac, but less so for running it on modern Ubuntu. There are several moving parts here, so once I had figured them out, I wanted to record my notes to save others some of the same aggravation.</p>
<p>Note that there are some other issues and differences between MySQL and PostgreSQL &#8211; for example see: <a href="http://blog.tiagocardoso.eu/rubyonrail/2008/02/20/porting-to-postgres-on-rails/" target="_blank">http://blog.tiagocardoso.eu/rubyonrail/2008/02/20/porting-to-postgres-on-rails/</a></p>
<p>In particular, one difference noted there between PostgreSQL and other SQL&#8217;s is that PostgreSQL is stricter about the difference between single and double quotes.  Double quotes are for &#8220;delimited identifiers&#8221;, such as table and column names, and prevent them from being mistaken for keywords.  For example, &#8220;SELECT&#8221; could be the name of a table or column or variable, whereas SELECT is an SQL keyword.  Single quotes are for string constants.  Use two adjacent single quotes for a literal single quote, as in &#8216;Dianne&#8221;s horse&#8217;.  Where this will get you is if you use double quotes in :conditions=&gt;&#8221;" and :joins=&gt;&#8221;", which will work in MySQL but not PostgreSQL.  Another difference is that &#8220;like&#8221; may need to be changed to &#8220;ilike&#8221; in PostgreSQL if you want case insensitive queries.</p>
<p>This post doesn&#8217;t attempt to address all issues, but just to get a system from a base Ubuntu Hardy (8.04 LTS) to a working Ruby on Rails 2.2/PostgreSQL 8.3 system.  This will also install working sqlite3 and postgresql drivers, and will test the installation as we proceed.</p>
<p>It also doesn&#8217;t attempt to address migration of data; do a web search on &#8220;mysql postgresql yml&#8221; to see several alternatives here.</p>
<p>(Some of these installation instructions are modified from Agile Web Development with Rails, third edition beta, which I assume you already have)</p>
<blockquote><p><code>apt-get update</code><br />
<code>apt-get upgrade</code><br />
<code>aptitude install build-essential</code></p></blockquote>
<p>if aptitude is not installed, that will cause an error.  Install with:</p>
<blockquote><p><code>apt-get install aptitude<br />
</code></p></blockquote>
<p>Now:</p>
<blockquote><p><code>aptitude install ruby rubygems ruby1.8-dev libsqlite3-dev</code><br />
<code>gem update --system<br />
</code></p></blockquote>
<p>At the end of a lot of output, was the notice that</p>
<blockquote><p><code>RubyGems installed the following executables:</code></p>
<p><code>/usr/bin/gem1.8</code><br />
<code>If 'gem' was installed by a previous RubyGems installation, you may need to</code><br />
<code>remove it by hand</code></p></blockquote>
<p>In my case, I did have to remove the old &#8216;gem&#8217; file by hand:</p>
<blockquote><p><code>mv /usr/bin/gem /usr/bin/gem.old</code><br />
<code>mv /usr/bin/gem1.8 /usr/bin/gem<br />
</code></p></blockquote>
<p>If you get the error about the uninitialized constant Gem::GemRunner(NameError), this is your problem</p>
<p>Then:</p>
<blockquote><p><code>gem install -y rails<br />
</code></p></blockquote>
<p>if you get an error that &#8220;could not find rails (&gt;0) in any repository&#8221;, simply try again</p>
<blockquote><p><code>gem install -y rails<br />
</code></p></blockquote>
<p>To use irb, you need:</p>
<blockquote><p><code>aptitude install irb<br />
</code></p></blockquote>
<p>if you want git:</p>
<blockquote><p><code>aptitude install git-core git-doc<br />
</code></p></blockquote>
<p>if you want apache:</p>
<blockquote><p><code>aptitude install apache2.2-common<br />
</code></p></blockquote>
<p>For passenger:</p>
<blockquote><p><code>gem install passenger<br />
passenger-install-apache2-module<br />
</code></p></blockquote>
<p>You may get some instructions about additional software to install for the passenger apache2 module to be compiled.  You will also get some instructions for configuring passenger to work under apache2.  Be aware that, with Ubuntu, you are encouraged NOT to edit the apache2.conf file, which may need updating with a new version of Ubuntu, but rather to edit other files included by apache2.conf, such as httpd.conf and the sites-available files (linked into sites-enabled when you want them to be enabled).</p>
<p>To use sqlite3 (e.g., for initial testing)</p>
<blockquote><p><code>gem install sqlite3-ruby<br />
</code></p></blockquote>
<p>For PostgreSQL:</p>
<blockquote><p><code>aptitude install postgresql postgresql-client<br />
</code></p></blockquote>
<p>Now, in order to access PostgreSQL, you need to have a PostgreSQL user defined, as well as a PostgreSQL database defined.</p>
<p>The PostgreSQL installation creates the &#8216;postgres&#8217; Linux user, the &#8216;postgres&#8217; PostgreSQL user, and the &#8217;postgres&#8217; database, so to get into the database, you can just (from root):</p>
<blockquote><p><code>su postgres</code><br />
<code>psql</code></p></blockquote>
<p>and poke around (psql has pretty good help &#8211; use l to list databases, du to list users, ? for help, and q to quit.)</p>
<p>Exit psql with &#8216;q&#8217;</p>
<p>To create a PostgreSQL user so you can test rails with PostgreSQL (in my case, I created user &#8216;nachbar&#8217;, since that is my Linux username) FROM THE SHELL (not from psql):</p>
<blockquote><p><code>su postgres<br />
</code><code>createuser nachbar<br />
</code></p></blockquote>
<p>(answer &#8216;y&#8217; to the question about being a superuser)</p>
<p>If you get an error that, for example &#8216;Ident authentication failed for user &#8220;xxxx&#8221; &#8216;, that means you forgot the &#8216;su postgres&#8217;.  Ident authentication means that PostgreSQL will allow Linux user &#8216;postgres&#8217; in because there is also a PostgreSQL user &#8216;postgres&#8217;</p>
<p>Once you have created your user (in my case, &#8216;nachbar&#8217;), AS THAT USER, try:</p>
<blockquote><p><code>psql postgres<br />
</code></p></blockquote>
<p>Here, &#8216;postgres&#8217; is the DATABASE name to which you are connecting.  If you don&#8217;t specify a database name, psql will try to connect to a database with the same name as your username, which does not exist.  (try just &#8216;psql&#8217; here to see that error)</p>
<p>Once you have psql working and your user set up in PostgreSQL, create a test rails application and test sqlite3 &#8212; as your own user (i.e., not root):</p>
<blockquote><p><code>rails test<br />
cd test<br />
script/generate model product title:string<br />
rake db:create<br />
rake db:migrate<br />
script/console<br />
t=Product.first<br />
</code>(that should return nil, since there are no products saved yet)<code><br />
p=Product.new<br />
p.title="My Title"<br />
p.save<br />
t=Product.first<br />
t.title<br />
</code></p></blockquote>
<p>The last command should read you  back &#8220;My Title&#8221; from your saved Product</p>
<p>Now, exit the console, and switch your app to PostgreSQL</p>
<blockquote><p><code>exit<br />
</code></p></blockquote>
<p>edit config/database.yml:</p>
<p>under <strong>development:</strong>, change adapter to &#8216;postgresql&#8217; and database to &#8216;test_development&#8217;.  No need to set a username, password, or anything else</p>
<p>Install the postgresql adaptor (as root)</p>
<p>First: install the postgreSQL header files:</p>
<blockquote><p><code>aptitude install libpq-dev<br />
gem install postgres<br />
</code></p></blockquote>
<p>Then, test it:</p>
<blockquote><p><code>irb<br />
require 'rubygems'<br />
require 'postgres'<br />
</code></p></blockquote>
<p>Now, (back as your own user, not root, and in the rails test project directory): create the PostgreSQL database:</p>
<blockquote><p><code>rake db:create<br />
rake db:migrate</code></p></blockquote>
<p>Test that these were created in PostgreSQL:</p>
<blockquote><p><code>psql test_development<br />
l  (to list databases)<br />
dt  (to list tables - should include the products table)<br />
q  (to exit psql)<br />
</code></p></blockquote>
<p>Run the same &#8220;script/console&#8221; test above, which should give the same results as it did with sqlite3.</p>
<p>Check the PostgreSQL database:</p>
<blockquote><p><code>psql test_development<br />
select * from products;<br />
</code>(don&#8217;t forget the semicolon.  Should show your your &#8220;My Title&#8221; product, now in PostgreSQL)<code><br />
q<br />
</code></p></blockquote>
<h2>Rails is running with PostgreSQL!</h2>
<p>Note that we did not set a user or password in database.yml, because we had created the &#8216;nachbar&#8217; user as a PostgreSQL superuser, and that was the user that script/console and rake were running as.  We used &#8216;Ident&#8217; authentication in this case.  There are several choices here, including creating another PostgreSQL user under which Rails will run.  Since &#8216;nachbar&#8217; is now a PostgreSQL superuser, you can run the createuser command as &#8216;nachbar&#8217; or &#8216;postgres&#8217;, but not as root!  In PostgreSQL, if the password is null, password authentication will always fail.</p>
<h3>Other miscellaneous notes</h3>
<h4>PostgreSQL configuration notes:</h4>
<p>PostgreSQL is set up to allow multiple &#8220;clusters&#8221;.  Installation creates a single cluster, &#8220;main&#8221;, which will probably be all you need.  In the following, &#8220;main&#8221; could refer to multiple directories if you have multiple clusters.  Also &#8220;8.3&#8243; is my PostgreSQL version number.  Other versions will, of course, have different directory names.</p>
<p>PostgreSQL configuration goes into /etc/postgresql/8.3/main and /etc/postgresql-common</p>
<p>PostgreSQL bin is in /usr/lib/postgresql/8.3/bin .  That directory is NOT added to the PATH, but appropriate links for psql, createuser, etc. are placed into /usr/bin.  Other commands, such as pg_ctl may not be in the path.  The base path for the Ubuntu bash shell is set in /etc/login.defs file in the ENV_SUPATH and ENV_PATH vars</p>
<p>The data directory is /var/lib/postgresql/8.3/main &#8212; see /var/lib/postgresql/8.3/main/postmaster.opts</p>
<p>According to /etc/init.d/postgresql-8.3, environment vars are set in /etc/postgresql/8.3/&lt;cluster&gt;/environment</p>
<p>possible options to /etc/init.d/postgresql-8.3 are:</p>
<p>start, stop, restart, reload, force-reload, status, autovac-start, autovac-stop, autovac-restart</p>
<p>(the functions are sourced from /usr/share/postgresql-common/init.d-functions)</p>
<p>On init, the init.d script looks for directories in /etc/postgresql/&lt;version&gt; (by default, &#8216;main&#8217; exists there) then, in those directories, look for postgresql.conf, which is the file that sets the data directory (/var/lib/postgresql/8.3/main), and the hba_file and ident_file (in /etc/postgreql/8.3/main), port, etc., as well as all sorts of configuration FOR THE SERVER</p>
<p>start.conf determines whether the specific server gets started on bootup</p>
<p>to backup:</p>
<p>pg_dumpall &gt; outputfile</p>
<p>to stop the server:</p>
<p>pg_ctl stop</p>
<p>some samples in</p>
<p>/usr/share/postgresql/8.3</p>
<h4>Rake and Rails data on PostgreSQL</h4>
<p>The &#8216;postgresql&#8217; database driver supports rake commands such as</p>
<blockquote><p><code><br />
rake db:drop<br />
rake db:create<br />
rake db:schema:load RAILS_ENV=production<br />
</code></p></blockquote>
<p>Be aware that PostgreSQL does not use autoincrement fields, but rather implements a more structured system using PostgreSQL sequences. Rails will create these for you, and will tell you about it. Thus, the rake db:schema:load will produce messages like:</p>
<blockquote><p><code><br />
-- create_table("appts", {:force=&gt;true})<br />
NOTICE: CREATE TABLE will create implicit sequence "appts_id_seq" for serial column "appts.id"<br />
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "appts_pkey" for table "appts"<br />
</code></p></blockquote>
<p>Those notices tell you that this mechanism is working properly.</p>
<h4>Rails, Passenger, and PostgreSQL users</h4>
<p>As indicated in the Passenger docs, passenger will run as the user that owns config/environment.rb, but that can be changed as indicated in the User Switching section of the Passenger docs, and can be modified by the PassengerUserSwitching and PassengerDefaultUser options in your httpd.conf or apache2 site files. Whichever user Passenger runs as must have a PostgreSQL user with the appropriate rights. Options include making that user a PostgreSQL superuser, or instituting better access controls with SQL GRANT commands.</p>
<p>In addition, options include other than the &#8220;Ident&#8221; mechanism of logging into PostgreSQL that we have discussed above. See the <a href="http://postgresql.org" target="_blank">PostgreSQL website</a> for details.</p>
<p>As one example, you can create the group and user &#8220;passenger&#8221; to use for passenger to run as, including the PostgreSQL user.:</p>
<blockquote><p><code>adduser --system --group passenger<br />
</code></p></blockquote>
<p>Change the group for the railsapp directory files by cd to the railsapp directory and issuing</p>
<blockquote><p><code>chgrp -R passenger *<br />
</code></p></blockquote>
<p>Change the mode for the log files and directory, so that the group (now &#8216;passenger&#8217;) can change those</p>
<blockquote><p><code>cd log<br />
chmod g+w .<br />
chmod g+w *<br />
</code></p></blockquote>
<p>Create the PostgreSQL user passenger:</p>
<blockquote><p><code>su postgres<br />
createuser passenger<br />
</code></p></blockquote>
<p>(answer &#8216;n&#8217; to all three questions: superuser, create databases, create roles)</p>
<p>Grant access to the passenger PostgreSQL user:</p>
<blockquote><p><code>su postgres<br />
psql<br />
c myrailsapp_production<br />
grant all on audits, sessions, users, mymodel1s, mymodel2s to passenger;<br />
grant all on sequence audits_id_seq, sessions_id_seq, users_id_seq, mymodel1s_id_seq, mymodel2s_id_seq to passenger;<br />
q<br />
</code></p></blockquote>
<p>Either change the owner of config/environment.rb to passenger, or set PassengerDefaultUser to passenger</p>
<p>Now Passenger will run as the &#8216;passenger&#8217; user, and will also set the effective group to the default group of the &#8216;passenger&#8217; user (also passenger, in this setup). It will access PostgreSQL as the PostgreSQL passenger user, as well, using ident authentication.  Of course, ident authentication works only within a single machine.  To access PostgreSQL from another machine, set the hostname, username, password, and port in Rails.</p>
<p>touch tmp/restart.txt to restart Passenger on the next request.</p>
<h4>Setting timezone on Ubuntu (different than setting it for your Rails app)</h4>
<p>ln -sf /usr/share/zoneinfo/America/Phoenix /etc/localtime</p>
<h4>Setting up the mail server on Ubuntu so Action Mailer Works:</h4>
<p>Mail: exim4 was already running, but would not deliver except locally.  Make changes to /etc/exim4/update-exim4.conf.conf &#8211; esp change configtype to &#8216;internet&#8217; (so mail can go out to the internet) but leave local_interfaces at &#8217;127.0.0.1&#8242; so mail will be accepted only from the local system.  Also change readhost to &#8216;myhostnamehere.com&#8217; so headers show that as the origin, and hide_mailname so readhost works.  Also, change /etc/mailname to mydomainnamehere.com, to indicate the domain of the user sending the mail.</p>
<h2>The Virtual Server</h2>
<p>To reproduce what I have done, I Actually implemented the above on the 1&amp;1 VPS I Linux package imaged to Ubuntu 8.04 LTS (64 bit). I think you can get a discount on that if you click this link:<br />
<a href="http://www.1and1.com/?k_id=8266491" target="_blank"><img src="http://banner.1and1.com/xml/banner?size=2%26%number=1" alt="Banner" width="468" height="60" border="0" /></a></p>
<h2>Happy Hacking!</h2>
]]></content:encoded>
			<wfw:commentRss>http://www.nachbar.name/blog/2008/11/28/rails-and-postgresql-on-ubuntu-hardy-804-lts/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Flash Player Bug with RoR 2:HTTPService fires fault by http status code 201</title>
		<link>http://www.nachbar.name/blog/2008/06/14/flash-player-bug-with-ror-2httpservice-fires-fault-by-http-status-code-201/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=flash-player-bug-with-ror-2httpservice-fires-fault-by-http-status-code-201</link>
		<comments>http://www.nachbar.name/blog/2008/06/14/flash-player-bug-with-ror-2httpservice-fires-fault-by-http-status-code-201/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 19:40:35 +0000</pubDate>
		<dc:creator>nachbar</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Adobe Flex Programming]]></category>
		<category><![CDATA[Ruby on Rails Programming]]></category>

		<guid isPermaLink="false">http://nachbar.name/blog/2008/06/14/flash-player-bug-with-ror-2httpservice-fires-fault-by-http-status-code-201/</guid>
		<description><![CDATA[Regarding Flexible Rails: Flex 3 on Rails 2, by Peter Armstring, and its Forum This relates to a previous thread, but the solution is buried deep within the thread. There is a bug in Flash Player, which has been reported: http://bugs.adobe.com/jira/browse/SDK-14153 Adobe considers this bug report &#8220;closed&#8221; with the &#8220;resolution&#8221; of &#8220;cannot fix&#8221;. Basically, Flash [...]]]></description>
			<content:encoded><![CDATA[<p>Regarding Flexible Rails: Flex 3 on Rails 2, by Peter Armstring, and its Forum</p>
<p>This relates to a previous thread, but the solution is buried deep within the thread. There is a bug in Flash Player, which has been reported:</p>
<p><a href="http://bugs.adobe.com/jira/browse/SDK-14153">http://bugs.adobe.com/jira/browse/SDK-14153</a></p>
<p>Adobe considers this bug report &#8220;closed&#8221; with the &#8220;resolution&#8221; of &#8220;cannot fix&#8221;. Basically, Flash Player HTTPService incorrectly faults on status code 201, which indicates &#8220;successful creation&#8221;. The Rails 2 scaffolding code returns this status code 201 on successful creation, triggering the fault event from HTTPService, and preventing the code in CreateLocationCommand.as on page 318 (for example) from working.</p>
<p>Since Adobe has given up on fixing this error, a workaround is required. One workaround would be to intercept the fault event, locate the status code 201, and treat it as &#8220;success&#8221;. However, I cannot find the status code in the fault event (!). You could also just treat the fault as a &#8220;success&#8221;, but then you wouldn&#8217;t know whether the create was successful.</p>
<p>The best workaround seems to be to change the status code returned from 201 to 200. This can be done in the rails controller. In this case, using iteration 8 code, pomodo/app/controllers/locations_controller.rb, line 55, change &#8220;:created&#8221; to &#8220;:ok&#8221; and CreateLocationCommand.as will work again.</p>
<p>James Nachbar<br />
<a href="http://www.plastic.org/">http://www.plastic.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nachbar.name/blog/2008/06/14/flash-player-bug-with-ror-2httpservice-fires-fault-by-http-status-code-201/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flex-Rails:protect_from_forgery problem with Rails 2.1 produces ioError 2032</title>
		<link>http://www.nachbar.name/blog/2008/06/14/flex-railsprotect_from_forgery-problem-with-rails-21-produces-ioerror-2032/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=flex-railsprotect_from_forgery-problem-with-rails-21-produces-ioerror-2032</link>
		<comments>http://www.nachbar.name/blog/2008/06/14/flex-railsprotect_from_forgery-problem-with-rails-21-produces-ioerror-2032/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 19:36:12 +0000</pubDate>
		<dc:creator>nachbar</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Adobe Flex Programming]]></category>
		<category><![CDATA[Ruby on Rails Programming]]></category>

		<guid isPermaLink="false">http://nachbar.name/blog/2008/06/14/flex-railsprotect_from_forgery-problem-with-rails-21-produces-ioerror-2032/</guid>
		<description><![CDATA[Update for Rails 2.2: According to the release notes: &#8220;Request forgery protection has been tightened up to apply to HTML-formatted content requests only&#8221; in Rails 2.2 &#8212; I have not tested this, but it should obviate the problem addressed in this post for Rails 2.2 and newer. Regarding Flexible Rails: Flex 3 on Rails 2, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update for Rails 2.2: According to the release notes: &#8220;Request forgery protection has been tightened up to apply to HTML-formatted content requests only&#8221; in Rails 2.2 &#8212; I have not tested this, but it should obviate the problem addressed in this post for Rails 2.2 and newer.</strong></p>
<p>Regarding Flexible Rails: Flex 3 on Rails 2, by Peter Armstrong:</p>
<p>The book talks about commenting out protect_from_forgery, and then uncommenting it in iteration 5 without mentioning what had changed to allow protect_from_forgery to be used.</p>
<p>In reviewing old vs. new rails code (particularly vendor/rails/actionpack/lib/action_controller/request_forgery_protection.rb), it appears that the older versions of rails did not run the forgery protection check for .xml requests, but the newer versions do. Thus, unless you are manually adding the appropriate parameters (see the above file for the current test being done to see if the form request is forged), you will fail the forgery test unless you prevent the test from running. More info on that here:</p>
<p><a href="http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-cross-site-request-forging-prevention">http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-cross-site-request-forging-prevention</a></p>
<p>at a minimum you will need:<br />
skip_before_filter :verify_authenticity_token<br />
in your sessions_controller.rb to avoid the ioError 2032.</p>
<p>You can track this error down by adding a fault event handler to the HTTPService (e.g. in LoginBox.mxml on page 153). You can also look at the output from the server (the &#8220;ruby scriptserver&#8221; command) which will show status code 422 instead of 200 for the &#8220;session.xml&#8221; request.</p>
<p>For a more detailed look, go to the rails log at logdevelopment.log and look at the end for the most recent error. It will show that ActionController::InvalidAuthenticityToken was thrown by /vendor/rails/actionpack/lib/action_controller/request_forgery_protection.rb:86:in `verify_authenticity_token&#8217;</p>
<p>CSRF attacks are not so relevant for applications running within Flash Player (as opposed to, for example, applications running within a browser), since Flash Player won&#8217;t go from one site to another.</p>
<p>If you want to continue to use forgery protection for the .html requests, the best solution is to</p>
<p>1) uncomment protect_from_forgery (so the protection token is generated),</p>
<p>2) skip_before_filter :verify_authenticity_token in the controllers that need to allow .xml to be served without the forgery protection, and then</p>
<p>3) call &#8220;verify_authenticity_token&#8221; (the same call used by request_forgery_protection.rb) within the .html generation code that you want to protect. verify_authenticity_token will throw the InvalidAuthenticityToken exception if the token is not correct.</p>
<p>If you want to protect your .xml calls too, the check within verify_authenticity_token is:<br />
form_authenticity_token == params[request_forgery_protection_token]<br />
so you would need to get your rails app to send the form_authenticity_token to the Flex client when the session is created, and then your subsequent calls will need to set the &#8220;request_forgery_protection_token&#8221; param.</p>
<p>James Nachbar<br />
<a href="http://www.plastic.org/">http://www.plastic.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nachbar.name/blog/2008/06/14/flex-railsprotect_from_forgery-problem-with-rails-21-produces-ioerror-2032/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flex-Rails: Non-Debug Flash Player caches, so fails to update list &#8211; status code 304</title>
		<link>http://www.nachbar.name/blog/2008/06/14/flexrails-non-debug-flash-player-caches-so-fails-to-update-list-status-code-304/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=flexrails-non-debug-flash-player-caches-so-fails-to-update-list-status-code-304</link>
		<comments>http://www.nachbar.name/blog/2008/06/14/flexrails-non-debug-flash-player-caches-so-fails-to-update-list-status-code-304/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 19:33:29 +0000</pubDate>
		<dc:creator>nachbar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Adobe Flex Programming]]></category>
		<category><![CDATA[Ruby on Rails Programming]]></category>

		<guid isPermaLink="false">http://nachbar.name/blog/2008/06/14/flexrails-non-debug-flash-player-caches-so-fails-to-update-list-status-code-304/</guid>
		<description><![CDATA[Regrading Flex/Ruby on Rails Programming: And then just when everything was working in the debug Flash Player, I decided to fire-up IE &#38; run the application in Flash Player in non-debug mode, and it stopped working: after creating an item, the list blanked out rather than being updated. Ultimately, the problem was that, in non-debug [...]]]></description>
			<content:encoded><![CDATA[<p>Regrading Flex/Ruby on Rails Programming:</p>
<p>And then just when everything was working in the debug Flash Player, I decided to fire-up IE &amp; run the application in Flash Player in non-debug mode, and it stopped working: after creating an item, the list blanked out rather than being updated.</p>
<p>Ultimately, the problem was that, in non-debug mode, using IE (but apparently not Firefox), Flash issued a &#8220;conditional get&#8221;, and was getting a 304 &#8220;not modified&#8221; response instead of the updated data. In debug mode, Flash was issuing a regular GET, and thus got the correct info. Thus, the application worked in debug mode, but not in non-debug mode.</p>
<p>I have seen that RoR 2.1 included some new caching functionality, although I don&#8217;t know if this is the kind of caching they are talking about, or why rails was reporting &#8220;not modified&#8221; even after the database upon which the response was based had been modified..</p>
<p>That Rails was returning status code 304 could be seen in the server window (&#8220;ruby scriptserver&#8221;)</p>
<p>For some reason, even though I am creating a new HTTPService object for each call, the return from the POST (i.e., the one object being created) was still being returned in the result event when I sent a GET to obtain the entire list. I could determine that by sending the result event info from the list command to the debug window:</p>
<p>var x:XMLList = XMLList(event.result.children());<br />
Pomodo.debug(x);</p>
<p>Even though this was the result of the GET call, I was still getting the result of the POST.</p>
<p>My fix (actually more of a workaround) was to add a time-generated string (&#8220;?&#8221; + Number(new Date()) ) to the end of the request URI, thus avoiding the caching problem. A better solution might be to send a &#8220;no-cache&#8221; header from the RoR portion, although I have not tested that. More on avoiding caching here:</p>
<p><a href="http://www.ruby-forum.com/topic/76658">http://www.ruby-forum.com/topic/76658</a></p>
<p>More evil IE caching, I guess!</p>
<p>James Nachbar<br />
<a href="http://www.plastic.org/">http://www.plastic.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nachbar.name/blog/2008/06/14/flexrails-non-debug-flash-player-caches-so-fails-to-update-list-status-code-304/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

