Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Saturday, April 26, 2008

will_paginate gem

While upgrading an old project to Rails 2.0.2 (and having trouble getting the will_paginate plugin to install), I discovered that there is a will_paginate gem:

http://mislav.caboo.se/rails/will_paginate-love/

Railscasts - will_paginate

Thursday, April 24, 2008

Rails - ActionController::InvalidAuthenticityToken


ActionController::InvalidAuthenticityToken in User#login

Showing user/login.html.erb where line #2 raised:

No :secret given to the #protect_from_forgery call.
Set that or use a session store capable of generating its
own keys (Cookie Session Store).

This happened after I switched from using the file system to a database for storing sessions (after uncommenting the line "config.action_controller.session_store = :active_record_store" in config/environment.rb).

Once you switch to database store, you'll need to comment the "protect_from_forgery" line in app/controllers/application.rb

# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
#protect_from_forgery # :secret => 'b3e36f40312f075ea2697fb85180e312'

Rails 2.0.2 - setup with functional tests

If you want to use setup with functional tests in Rails 2.0.2, you'll need to rename the setup method to setup_with_fixtures and call super before running your setup code:
  
def setup_with_fixtures
super
# Set up code here
end

Sunday, March 23, 2008

Ruby on Rails - Using External Asset Hosts

Rails uses the "public" directory on the current host as the default location for static assets (images, CSS, Javascript, etc). Setting up your Rails app to use an external assets server is as simple as adding a single line to a configuration file.

Add the following line to your config/environment.rb file:

ActionController::Base.asset_host = 'http://assets.yourdomain.com'

It's also possible to set a single environment to use an external asset host:

Add the following line to config/environments/production.rb (the most likely spot - this line should already be in this file, but commented out):

config.action_controller.asset_host = 'http://assets.yourdomain.com'

Now this line in my layout

<%= stylesheet_link_tag 'scaffold/base' %>

will translate to http://assets.yourdomain.com/stylesheets/scaffold/base.css

Additional information on setting up multiple asset hosts can be found at:

http://api.rubyonrails.com/classes/ActionView/Helpers/AssetTagHelper.html

Wednesday, March 12, 2008

Ruby Gem - Highline

I was looking for a way to prompt a user for input on the console and ran across the RubyGem HighLine. It does a lot more than this, but here's how to capture STDIN from the console:

#!/usr/local/bin/ruby
require 'rubygems'
require 'highline/import'

username = ask("Enter your username: ") { |q| q.echo = true }
password = ask("Enter your password: ") { |q| q.echo = "*" }

Here's the output on the console:

$ ruby highline.rb
Enter your username: doug
Enter your password: ******

Get it here:

HighLine

or simply install the gem the normal way:

$ sudo gem install highline

Autotesting Javascript in Rails

From Dr. Nic's Blog:

Autotesting Javascript in Rails

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels