Good notes posted by neves

RSS feed
September 17, 2008
4 thanks

Turn layout off with render

Thats awkward, but the code below does not turn layout off:

render :action => "short_goal", :layout => nil

you must use false

render :action => "short_goal", :layout => false
August 29, 2008
6 thanks

Brazilian Real (R$ 1.200,95)


def number_to_currency_br(number)
  number_to_currency(number, :unit => "R$ ", :separator => ",", :delimiter => ".")
August 25, 2008
5 thanks

%w(true false) != [true, false]

Don´t confuse the right:

validates_inclusion_of :published, :in => [true, false]

with the wrong:

validates_inclusion_of :published, :in => %w(true false)


%w(true false) == ["true", "false"]
August 19, 2008
6 thanks

script/generate syntax

To add a post_id field to a comments table, run this:

script\generate migration add_post_id_to_comment post_id:integer

See that it´s not the table name(plural), but the model name(singular),<br /> and post_id:references, does not works like in create_table.

This is the generated migration:

class AddPostIdToComment < ActiveRecord::Migration
 def self.up
   add_column :comments, :post_id, :integer

 def self.down
   remove_column :comments, :post_id
August 14, 2008 - (<= v2.1.0)
7 thanks

with password md5 encrypted

If you are afraid to let your plain password on the code, you can do this instead:

 require 'digest'

 class AdminController < ApplicationController
  before_filter :authenticate

  def authenticate
    authenticate_or_request_with_http_basic('Administration') do |username, password|
      md5_of_password = Digest::MD5.hexdigest(password)
      username == 'admin' && md5_of_password == '5ebe2294ecd0e0f08eab7690d2a6ee69'

where ‘5ebe2294ecd0e0f08eab7690d2a6ee69’ is the md5 of the word ‘secret’.

You can get your own with this free webservice: <br /> (replace ‘secret’ with your secret word).