<?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>Ahabman &#187; rails mod_rails capistrano</title>
	<atom:link href="http://ahabman.com/blog/tag/rails-mod_rails-capistrano/feed/" rel="self" type="application/rss+xml" />
	<link>http://ahabman.com/blog</link>
	<description></description>
	<lastBuildDate>Sun, 27 Nov 2011 19:52:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>mod_rails set RAILS_ENV variable to QA, Staging, or Production</title>
		<link>http://ahabman.com/blog/2008/05/mod_rails-set-rails_env-variable-to-qa-staging-or-production/</link>
		<comments>http://ahabman.com/blog/2008/05/mod_rails-set-rails_env-variable-to-qa-staging-or-production/#comments</comments>
		<pubDate>Sat, 24 May 2008 01:32:14 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[rails mod_rails capistrano]]></category>

		<guid isPermaLink="false">http://www.grandrapidswebdeveloper.com/blog/2008/05/mod_rails-set-rails_env-variable-to-qa-staging-or-production/</guid>
		<description><![CDATA[I&#8217;m using mod_rails with capistrano and multi-stage (qa, staging, production), and wrote this workaround so that the RAILS_ENV could be set correctly in each stage. Mod_rails sets RAILS_ENV once in the global server conf file, but I needed it set once for each environment: qa, staging, and production. I tried setting ENV['RAILS_ENV] in each file [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using mod_rails with capistrano and multi-stage (qa, staging, production), and wrote this workaround so that the RAILS_ENV could be set correctly in each stage.  Mod_rails sets <a href="http://www.modrails.com/documentation/Users%20guide.html#rails_env" target="_blank">RAILS_ENV once in the global server conf file</a>, but I needed it set once for each environment: qa, staging, and production.  I tried setting ENV['RAILS_ENV] in each file under config/deploy/ but the setting was not picked up.</p>
<p>Solution &#8211; write the correct value into config/environment.rb&#8217;s  ENV[''RAILS_ENV] while deploying.</p>
<p>So here is my 3 step fix -</p>
<p>Step 1.</p>
<p>I created a new file</p>
<p>lib/set_mod_rails_env.rb</p>
<pre class="brush: ruby; title: ; notranslate">
# sets ENV['rails_env'] for mod_rails
# &lt;a href=&quot;http://www.megasolutions.net/ruby/search-a-file-and-replace-text-50116.aspx&quot; target=&quot;_blank&quot;&gt;http://www.megasolutions.net/ruby/search-a-file-and-replace-text-50116.aspx&lt;/a&gt;
# Robert Evans'
def ChangeOnFile(file, regex_to_find, text_to_put_in_place)
  text = File.read file
  File.open(file, 'w+'){|f| f &amp;lt;&amp;lt; text.gsub(regex_to_find,
      text_to_put_in_place)}
end

ChangeOnFile(&quot;#{ARGV[0]}/config/environment.rb&quot;, /#mod_rails_env_here/, &quot;ENV['RAILS_ENV']='#{ARGV[1]}'&quot;)
</pre>
<p>Step 2.</p>
<p>Add the following code to config/deploy.rb</p>
<pre class="brush: ruby; title: ; notranslate">
namespace :deploy do
  desc &quot;set ENV['RAILS_ENV'] for mod_rails&quot;
  task :before_restart do
    run &quot;ruby #{current_release}/lib/set_mod_rails_env.rb  #{current_release}  #{stage}&quot;
  end
end
</pre>
<p>Step 3.</p>
<p>Add the following code to config/environment.rb</p>
<pre class="brush: ruby; title: ; notranslate">
# deploy.rb will swap this out for the appropriate rails_env.
# mod_rails apparently need this var in this file (not ./environments/qa.rb)
# do not change the following line. Ruby regexp looks for it.
#mod_rails_env_here
</pre>
<p>That&#8217;s it.  Let me know if you know of an easier way.</p>
]]></content:encoded>
			<wfw:commentRss>http://ahabman.com/blog/2008/05/mod_rails-set-rails_env-variable-to-qa-staging-or-production/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

