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

Rails+Mongrel+Apache 2 on Mac OSX Leopard

Apache posted 5 months ago by christian

I use this configuration on my development machine when I need mod_rewrite; it’s not meant for production:

   1  <VirtualHost *:80>
   2  ServerName dev.xxx.com
   3  
   4  # Enable URL rewriting
   5  RewriteEngine On
   6  
   7  # Rewrite index to check for static pages
   8  RewriteRule ^/$ /index.html [QSA]
   9  
  10  # Rewrite to check for Rails cached page
  11  RewriteRule ^([^.]+)$ $1.html [QSA]
  12  
  13  # Redirect all non-static requests to cluster
  14  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  15  RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
  16  
  17  DocumentRoot  "/Users/christian/Documents/Projects/xxx/public"
  18  <Directory "/Users/christian/Documents/Projects/xxx/public">
  19       Options Indexes FollowSymLinks
  20  
  21       AllowOverride None
  22       Order allow,deny
  23       Allow from all
  24   </Directory>
  25  
  26  </VirtualHost>
  27  
  28  <Proxy balancer://mongrel_cluster>
  29    BalancerMember http://127.0.0.1:3000
  30  </Proxy>

Tagged apache, mongrel, osx, rails

How to use ActiveRecord without Rails

Ruby posted 5 months ago by christian

This is an example of how to use ActiveRecord without Rails:

   1  ['/model', '/db'].each do |folder|
   2    $:.unshift File.dirname(__FILE__) + folder
   3  end
   4  
   5  require 'test/unit'
   6  require 'rubygems'
   7  require 'activerecord'
   8  
   9  ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
  10  ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/config/database.yml'))
  11  ActiveRecord::Base.establish_connection('sqlite3')
  12  
  13  require 'schema'
  14  

Schema contains, for example:

   1  ActiveRecord::Schema.define :version => 0 do
   2    create_table :languages, :force => true do |t|
   3      t.string :name
   4    end
   5  end

Tagged activerecord, standalone, rails, ruby

How to use named_scope in Rails

Ruby posted 5 months ago by christian

Simple example of how to use the named_scope feature:

   1  class Feed < ActiveRecord::Base
   2   
   3    named_scope :active, :conditions => "(active = 1)"
   4    named_scope :stale,  :conditions => ["last_updated > ?", 30.minutes.ago.to_s(:db)]

Usage:

   1  Feed.active # return the active feeds

Chaining is also possible:

   1  Feed.active.stale # return the feeds that need to be updated

Tagged named_scope, rails, activerecord, ruby

Showing ActiveRecord error messages from jQuery Ajax actions and scripts

HTML (Rails) posted 6 months ago by christian

The HTML , in a layout file, for example application.html.erb:

   1  <div id="error-message" style="display:none">
   2  </div>

The JavaScript, rendered by for example create.js.erb:

   1  <% if !@category.valid? %>
   2  <%
   3    errors = <<ERR
   4    <p>Please fix the following errors:</p>
   5    <ul>
   6      #{@category.errors.collect{|err| "<li>" + err[0] + " " + err[1] + "</li>" } }
   7    </ul>
   8  ERR
   9  %>
  10  $('#error-message').html('<%= escape_javascript(errors) %>');
  11  $('#error-message').show();
  12  <% else %>
  13  $('#error-message').hide();
  14  <% end %>

Tagged ajax, error, jquery, rails

How to use jQuery with Rails 2.0 - aka How to fix "ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)"

Ruby posted 6 months ago by christian

All credit goes to Henrik Nyh for writing a blog post about how to fix this issue.

This is a slight variation of his code:

In application.html.erb, or whatever layout file you’re using, put:

   1  <%= javascript_tag "window.AUTH_TOKEN = '#{form_authenticity_token}';" %>

In application.js, or whatever JavaScript file you’re using, put:

   1  $(document).ajaxSend(function(event, request, settings) {
   2    if (typeof(window.AUTH_TOKEN) == "undefined") return;
   3    settings.data = settings.data || "";
   4    settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
   5  });

That’s all…

Tagged authenticity, rails, rails 2, token, jquery, javascript, ajax