Register now and start sharing your code snippets.
-->

How to test Rails routing with RSpec

Ruby posted 2 months ago by christian

   1  route_for(:controller => 'categories', :action => 'show', :permalink => ['one', 'two', 'three']).should.equal '/one/two/three'
   2  route_for(:controller => 'categories', :action => 'index').should.equal '/'
   3        
   4  params_from(:get, "/").should.equal {:controller => "categories", :action => "index"}
   5  params_from(:get, "/something/").should.equal {:controller => "categories", :action => "show", :permalink => ["something"]}

The test_spec_on_rails plugin also allow you to test routing and much more, see the docs for more information.

Tagged testing, rspec, routing, routes

How to SEO optimize your Rails URLs and routes

Ruby posted 2 months ago by christian

My idea for achieving optimal content crawlability and SEO optimized URLs is to use permalinks instead of ids and the default Rails routes. The permalinks can contain whatever you decide is optimal from a SEO point of view.

As an example, let’s take a recipe site that has a recipe at http://xxx/recipes/asia/china/beijing-duck.html.

First let’s configure the .html extension to be handled by the RecipesController:

   1  map.connect 'recipes/*permalink.html', :controller => 'recipes', :action => 'show'

In the code we use the URI , which is the permalink of the recipe, to retrieve the recipe from the database:

   1  class RecipesController
   2    def show
   3      @product = Recipe.find_by_permalink(request.path)
   4    end
   5  end

To handle the categories and subcategories, we use the following route:

   1  map.connect 'recipes/*permalink/', :controller => 'categories', :action => 'show'

And create the CategoriesController:

   1  class CategoriesController
   2    def show
   3      @category = Category.find_by_permalink(request.path)
   4    end
   5  end

Now what’s left is for you to figure out how to generate the permalinks… I recommend having a look at permalink_fu.

Tagged seo, rails, routes, permalink, crawlability

How to localize Rails routes

CSS posted 2 months ago by christian

I forget this all the time:

   1  map.resources :products, :as => 'productos', :path_names => { :new => 'nuevo', :edit => 'editar' }

Documented here….

Tagged resources, routes, localize, rails

How to install the exception_logger Rails plugin and protect the logs with basic authentication

Ruby posted about 1 year ago by christian

This snippet explains how to install and use the Rails exception_logger plugin. I’ll also show you how to protect your logs by extending the plugin with basic authentication.

   1  script/plugin source http://svn.techno-weenie.net/projects/plugins
   2  script/plugin install exception_logger

I’m using Rails Edge on this project, so I had to install classic pagination also:

   1  script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination

Next create and execute the migration file:

   1  ./script/generate exception_migration
   2  rake db:migrate

Before starting the server we need to setup the routes:

   1  map.exceptions '/logged_exceptions/:action/:id', :controller => 'logged_exceptions', :action => 'index', :id => nil 

You also need to include the ExceptionLoggable in your ApplicationController:

   1  class ApplicationController < ActionController::Base
   2    include ExceptionLoggable
   3  ...

Start your server and access the exception log at /logged_exceptions.

Exceptions can contain email addresses, passwords, credit card numbers, so you’ll want to protect /logged_exceptions from the public. This can be done by adding the following code to the end of environment.rb:

   1  config.after_initialize do
   2    require 'application' unless Object.const_defined?(:ApplicationController)
   3    LoggedExceptionsController.class_eval do
   4      before_filter :authenticate
   5  
   6      protected
   7  
   8      def authenticate
   9        authenticate_or_request_with_http_basic do |username, password|
  10          username == "foo" && password == "bar"
  11        end
  12      end
  13    end
  14  end

With this code we add a before filter that shows a login dialog to anyone trying to access /logged_exception/. Note that this requires Rails 2.0 basic authentication to work, so make sure you have the proper version installed.

Tagged ruby, exception_logger, install, routes, rails, basic, authentication