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

How to run multiple Rails applications from the same directory

Ruby posted about 1 year ago by christian

Set this in environment.rb:

   1  ActionController::AbstractRequest.relative_url_root = "/appname/"
   2  ActionController::CgiRequest.relative_url_root = "/appname/"

Tagged rails, nginx, apache

Fixing "config.breakpoint_server has been deprecated and has no effect" when using Rails Edge

Ruby posted about 1 year ago by christian

You might get this error if you’re using Rails Edge: config.breakpoint_server has been deprecated and has no effect:

To fix the error we need to remove the following from config/environments/development.rb:

   1  config.breakpoint_server = true

Next install ruby-debug:

   1  gem install ruby-debug

Then add this to the end of config/environments/development.rb:

   1  require 'ruby-debug'

Next, start your server, and the error should be gone…

To debug your code just add a call to debugger:

   1  class MySillySpace ...
   2    def create
   3      debugger # add this line
   4    end

Now when you access the URL with your browser you’ll have access to the debugger from the console window.

To learn how to use ruby-debug, read this tutorial written by the ruby-debug author.

Tagged rails, edge, debug, deprecated

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

Fixing "warning: already initialized constant OPTIONS"

Shell Script (Bash) posted about 1 year ago by christian

If you’re using Edge Rails and mongrel you might have seen this error:

   1  warning: already initialized constant OPTIONS

There’s a bug report here that explains this problem in detail.

The solution for me was to rerun the freeze command:

   1  rake rails:freeze:edge

You might also get “warning: already initialized constant OPTIONS ” error if you haven’t installed a gem your software relies on. In the following example openssl should be installed:

   1  /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’: no such file to load—openssl (MissingSourceFile)

ActiveResource might also be the problem:

   1  gem install activeresource --source http://gems.rubyonrails.org

Tagged rails, edge, mongrel, mephisto

Using Rails helpers from controllers or anywhere you want

Ruby posted about 1 year ago by christian

Use this snippet if you need to use one of the many Rails helpers in controllers or elsewhere:

   1  class Helpers
   2      include Singleton
   3      include ActionView::Helpers::TextHelper
   4      include ActionView::Helpers::UrlHelper
   5      include ActionView::Helpers::DateHelper
   6      include ActionView::Helpers::TagHelper
   7      include ActionView::Helpers::ActiveRecordHelper
   8    end

For example, to obfuscate email use the mail_to helper and set the encoding to JavaScript:

   1  Helpers.instance.mail_to "me@domain.com", "My email", :encode => "javascript"

Tagged ruby, rails, helpers