Archive

Archive for the ‘Ruby on Rails’ Category

Multistage deployments with Capistrano

April 25th, 2009 Comments off

I have been trying to set up a Capistrano script for deploying to a staging and production server. I found this very well written tutorial showing how to set up the various cap scripts to work with mod_rails (Passenger).

One note, Capistrano does not copy over the database.yml file. You need to put this in yourself. This tripped me up for a while since I was assuming that it would somehow magically take the database.yml.template file I had and turn it into a database.yml file that I would then have to configure. The Ruby magic doesn’t extend that far ;-) .

Categories: Ruby on Rails, Web Development Tags:

Keep CSS layout in layouts not in view pages

April 16th, 2009 Comments off

When using layouts in Rails, I strongly suggest keeping as much of the container elements in the layout and using content_for to create sections in which content can be placed.

I am working on a site right now that has used the YUI Grids to layout the site (something I highly recommend for anyone designing a site from scratch). Most of the site uses a two column layout. The application.html.erb file looks like this:

<body>
  <div id="doc" class="yui-t6">
    <div id="hd" role="banner">
      <%= render_partial "shared/header" %>
    </div>

    <div id="bd" role="main">
      <%= yield :content %>
    </div>

    <div id="ft" role="contentinfo">
      <%= render_partial "shared/footer" %>
    </div>
  </div>
</body>

What is missing is the part that divides the page into your content area and your, in this case, right-hand side area. This part was placed in the rest of the view files. Not only does this mean that it is repeated everywhere and a huge pain to change, it also means that it is much harder for coders, who may not know YUI CSS Grids, to properly lay out pages. In the case of the app that I am modifying, it has also meant that parts of the CSS layout are strewn careless between regular pages and partials.

Instead, this is how the layout page should look:

<body>
  <div id="doc" class="yui-t6">
    <div id="hd" role="banner">
      <%= render_partial "shared/header" %>
    </div>

    <div id="bd" role="main">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g">
            <%= yield :content %>
          </div>
        </div>
      </div>
      <div class="yui-b">
        <%= yield :side_content %>
      </div>
    </div>

    <div id="ft" role="contentinfo">
      <%= render_partial "shared/footer" %>
    </div>
  </div>
</body>

This keeps everything clean and makes for view pages that look like the following.

<% content_for :content do %>
  put content here
<% end %>

<% content_for :side_content do %>
  put side content here
<% end %>

I think that makes it much easier to see what is happening within view pages. It also keeps all the layout code in one place.

Categories: Ruby on Rails, Web Development Tags:

Bort – a new way to start Rails projects

September 15th, 2008 Comments off

I found a great way of starting a new Rails project. It is called Bort and has been put together by Jim Neath. It includes RESTful Authentication, Will Paginate, RSpec and RSpec for Rails, Exception Notifier, Capistrano recipes and other Rails plug-ins in one small downloadable zip file. The uncompressed zip file then serves as the base for your new Rails app.

Categories: Ruby on Rails Tags:

How to get Phusion Passenger and ImageMagick to work together?

July 5th, 2008 9 comments

For Studio Gallery, I want people to have the ability to upload avatars for the clients that they create. I am using the Paperclip plug-in for this and when running the app via script/server this works great. I installed ImageMagick and Paperclip is able to find it since all the right paths are set in my .bash_profile file.

The problem is that I can’t get Paperclip or ImageMagick to work when running the app via Passenger. I have added the following lines to my /etc/profile file:

#ImageMagick
export MAGICK_HOME="/usr/local/ImageMagick-6.4.1"
export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib"
export PATH=$MAGICK_HOME/bin:/usr/bin:$PATH

But so far, it seems that Apache or Passenger is unable to find ImageMagick. Anyone have any ideas?

Categories: Apple Mac, Ruby on Rails Tags:

Installing Phusion’s Passenger on Leopard (OS X 10.5)

May 29th, 2008 10 comments

This is what I had to do to get Passenger (aka mod_rails) up and running on my Mac. My computer is running OS X 10.5 with a default Ruby and Rails install. The websites I referenced for all the steps are listed at the end.

Prerequisites:
You will need to have XCode installed as mod_passenger.so needs to be compiled.

Installing Passenger Gem

The first thing is to install and build the Passenger gem.

$ sudo gem install passenger
$ sudo passenger-install-apache2-module

The second line will compile mod_passenger.so and launch an installer script. At the end of the script there will be 3 lines that you need to add to your Apache configuration in order for anything to work. Copy these lines directly from the install script as they may be unique to your computer.

Configuring Apache httpd.conf

In this section, we need to configure Apache so it can properly use mod_rails and access your Virtual hosts, which we will setup later. The lines you copied in the previous step need to be added to one of two files – /etc/apache2/httpd-conf or your personal configuration file, in my case /etc/apache2/users/chrisjohnston.conf. it doesn’t matter which one you pick. In getting this all running, I used both files and things worked fine. In the end, I picked my personal config file because it kept things cleaner.

To open the file, use the following:

$ sudo vim /etc/apache2/users/chrisjohnston.conf

This is what my file looks like. I will explain the various parts after the snippet.

LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-1.0.5/ext/apache2/mod_passenger.so
RailsSpawnServer /Library/Ruby/Gems/1.8/gems/passenger-1.0.5/bin/passenger-spawn-server
RailsRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

RailsEnv development

<Directory "/Users/chrisjohnston/Sites/">
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

<Directory "/Users/chrisjohnston/Dev/">
    Order allow,deny
    Allow from all
</Directory>

The first three lines load mod_passenger.so and set it up. The RailsEnv development line is needed in order to run the server in development mode. If this is to be a production server, then you can leave this line out as it defaults to production.

The next important part of the file is

<Directory "/Users/chrisjohnston/Dev/">
    Order allow,deny
    Allow from all
</Directory>

This gives you access to all the files below the given directory. This is important if you want things like stylesheets or images to load. We will also come back to this when we set up virtual hosts for the site.

Setting up Virtual Hosts

The next thing to do is to set up a virtual host for your website. In order for this to work, first open the apache httpd.conf file

$ sudo vim /etc/apache2/httpd.conf

and ensure the following line is uncommented, otherwise, nothing else beyond this point will work.

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Once you have uncommented line, open up the httpd-vhosts.conf file and add a virtual host pointing to where you rails app is. Also, ensure that NameVirtualHost *:80 is uncommented.

$ vim /private/etc/apache2/extra/httpd-vhosts.conf

Here is what mine looks like:

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

# Rails apps
<VirtualHost *:80>
    DocumentRoot "/Users/chrisjohnston/Dev/studio_gallery/public"
    ServerName dev.studiogallery.com
    ErrorLog "/Users/chrisjohnston/Dev/studio_gallery/log/error.log"
</VirtualHost>

There may be two dummy virtual hosts listed in the file. Either comment these out or delete them. They will cause errors when you restart the server.

In the snippet above, you can see that I have set up a ServerName for my site. In order to navigate to this (without ending up searching for it on the net) we need to modify the /etc/hosts file. You need to add a line that tells your computer to associate the domain you created with your computer.

# Virtual hosts on this computer
127.0.0.1       dev.studiogallery.com

Now we can restart Apache.

$ sudo apachectl restart

And if everything goes well, when you navigate to your virtual site, your Rails app should come up.

From the highgroove Studio’s The Napkin website, I also added the following to my .bash_profile file:

# Use this in any RAILS_ROOT dir. That restart.txt file tells mod_rails to restart this app.
# You'll want to do this when (for example) you install a new plugin.
alias restart_rails='touch tmp/restart.txt'

# By default, your app's error log now goes here. Unless you configure your apps otherwise,
# it's helpful to have an alias to take you to your error log quickly.
alias apache_logs='cd /private/var/log/apache2/'

# You'll be adding to your vhosts configuration everytime you introduce a new Rails app.
# Might as well make it a shortcut
alias vhosts='sudo vi /private/etc/apache2/extra/httpd-vhosts.conf'

# Dito with hosts
alias hosts='sudo vi /etc/hosts'

# You'll need to restart apache whenever you make a change to vhosts.
# You can also click System Preference->Sharing->Web Sharing, but this is quicker.
alias apache_restart='sudo apachectl restart'

References:

Categories: Ruby on Rails Tags:

Starting open source github project – Studio Gallery

May 4th, 2008 Comments off

As part of a larger project, I am starting a new open source Github project – Studio Gallery. It is a Ruby on Rails application aimed at professional photographers.

The app will allow photographers to create galleries for clients, uploading photos from photo shoots. Once the gallery is created, clients can log on and select the photos they want to order. Other features include the ability to rate photos and filter by ratings as well as pick the size of print to be ordered.

The project is just beginning, so nothing exists for it yet except the Github repository. I will putting up a Trac site or Wiki for it soon.

Ruby and Upgrading to Leopard

December 6th, 2007 1 comment

I recently upgraded my mac to OS X 10.5 and had some problems installing the Ruby gem Hpricot. This particular gem is compiled from source when it is installed (on a Mac at least, no idea about Windows or Linux). When I tried to install it, it kept throwing an error and failing. The problem was that I had forgotten to also upgrade Xcode, so it was being compiled with an old copy of gcc.

Categories: Apple Mac, Ruby, Ruby on Rails Tags: