linz.id.au

Forgetting peoples names since 1978

Parasitic Rails apps

Had an interesting problem the other day – we launched a new Rails app (FiveToDo), and I needed to build a quick & dirty homepage + contact form site for the parent company, GRS.

The homepage itself is easy enough – just added another server section to my Nginx config, but the contact form turned out to be a bit of a pain.

Normally, this would be fairly easy if I was working with PHP/ASP/ColdFusion – a simple page that sends out an email & displays a thank you message, but creating a whole Rails/Camping/whatever app seemed like a bit of overkill, plus running another Mongrel instance on our VPS slice would probably tip us over the edge memory usage wise.

After posing this problem on #ror_au, lachie suggested whipping up a CGI script in Ruby to handle it, which worked a treat – until I realised that Nginx doesn’t do CGI.

My solution has been to have the GRS site piggyback on the FiveToDo site – I just added a few lines to controllers/application.rb:

1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt>
  before_filter <span class="sy">:catch_grs</span><tt>
</tt><tt>
</tt>  <span class="r">def</span> <span class="fu">catch_grs</span><tt>
</tt>    <span class="r">if</span> request.host.include?(<span class="s"><span class="dl">'</span><span class="k">globalreservationsolutions</span><span class="dl">'</span></span>) <tt>
</tt>        && !params[<span class="sy">:action</span>].include?(<span class="s"><span class="dl">'</span><span class="k">enquiry</span><span class="dl">'</span></span>) <span class="r">then</span><tt>
</tt>      render <span class="sy">:template</span> => <span class="s"><span class="dl">'</span><span class="k">grs/default</span><span class="dl">'</span></span>, <span class="sy">:layout</span> => <span class="pc">false</span><tt>
</tt>      <span class="r">return</span> <span class="pc">false</span><tt>
</tt>    <span class="r">end</span><tt>
</tt>  <span class="r">end</span><tt>
</tt>

Then I added an enquiry controller that sends the email & renders the thank you message or any errors.

Now, I’m sure this isn’t the best option, running the before filter on every request for the FiveToDo app can’t be all that efficient, but it was the best I could come up with on short notice.

So, does anyone have any tricks/tips they’d like to share on how to handle such cases?

Comments are disabled for this post