Ruby on rails: quick tips
17/12/07 00:15 Filed in: Ruby on rails
Tip 1: Guidelines on working with views
When working with views keep in mind:- You can generate models for yours views. The models will behave like base tables with the difference that you can't update rows.
- If your view doesn't have an id column remember to set the primary key explicitly using the ''set_primary_key'' method.
Tip 2: Accessing the controller and action names
Controller name:controller.controller_name
Action name:
controller.action_name
Tip 3: Dumping tables to YAML
While developing your Rails application you tend to first put data in your development database. If you want to use that data later in your unit test fixture, you can do so by using the runner script. Say you have a products table and a corresponding Product model class. You can dump the data in that table using$ ruby script/runner 'puts Product.find(:all).to_yaml'
Tip 4: Instatiated fixtures in Rails >= 1.0
If you're using Rails 1.0 or later and have tried accessing an instantiated fixture in your unit tests, you will probably be greeted by a nil object error similar to:$ ruby test/unit/product_test.rb
...
NoMethodError: You have a nil object when you didn't expect it!
Since version 1.0, Rails deactivates instantiated fixtures by default for performance reasons. If you have the following product in your fixture
phils_favourite_game:
id: 1
name: Civilization
Then you won't be able to access it with
@phils_favourite_game. Instead you must resort to products(:phils_favourite_game). The extra verbosity is the price paid for the performance gain.If you disdain needless verbosity or you don't want to break existing unit tests that use instantiated fixtures, you can reactive instantiated fixtures in
test/test_helper.rb:self.use_instantiated_fixtures = true
For more detailed information see Mike Clark's weblog (Mike is one of the authors of Agile Web Development with Rails).
Tip 5: Simple CAPTCHTAs
Requirements: rmagick.First create a controller for the CAPTCHA. In this controller we define a random method that generates the CAPTCHA image for us and returns it to the browser. In other words, when you point your browser to http://yourhost/image/random it will return you a CAPTCHA.
require 'RMagick'
include Magick
class ImageController < ApplicationController
# Creates an image from a random number and stores that random number in
# the flash
def random
# Create a random number between 0 and 9999, padding it with leading
# zeros as necessary to bring it to 4 digits
num = sprintf("%04d", rand(10000))
# Create the image with the random number
image = Image.new(90, 35)
text = Magick::Draw.new
text.pointsize = 36
text.annotate(image, 0, 0, 5, 30, num) { self.fill = 'black' }
# Store the string representation of the random number in the flash (by
# storing the string representation we also store any padded zeros)
flash[:random_num] = num
# Send the data to the browser
data = image.to_blob { self.format = "png" }
send_data(data,
:filename => "random.png",
:type => "image/png",
:disposition => "inline")
end
end
Note how we store the randomly generated number in the flash. We need to store it to compare it in the registration process:
def register
@user = User.new(params[:user])
# Tell the user if he entered the wrong access code
if params[:captcha] != flash[:random_num]
@user.valid?
@user.errors.add_to_base("Invalid access code")
render :action => 'join'
elsif ...
# remaining registration checks
# ...
end
|