Posts tagged as:

rails

Rails 2.0 Timestamps

February 19, 2008

No Gravatar

Thought timestamps were easy before? Timestamps in Rails 2.0 are super easy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# before
class CreateUsers < ActiveRecord::Migration
 def self.up
 create_table :users do |t|
 t.column :name, :string
 t.column :subscribed, :boolean, :default => true
 t.column :created_on, :timestamp
 t.column :updated_on, :timestamp
 end
 end
 
 def self.down
 drop_table :users
 end
end
 
# after
class CreateUsers < ActiveRecord::Migration
 def self.up
 create_table :users do |t|
 t.string :name
 t.boolean :subscribed, :default => true
 t.timestamps
 end
 end
 
 def self.down
 drop_table :users
 end
end

{ Comments }

Change Rails’ layout via controller

February 18, 2008

No Gravatar

I’ve seen a few people asking me how to specify a specific layout in their controllers. Simple:

1
2
3
4
#my admin/condos_controller.rb
class Admin::CondosController < ApplicationController
  layout 'layouts/admin'
end

{ Comments }

:status => 301 and : moved_permanently redirects in Rails

February 11, 2008

No Gravatar

I made a recent post on 301 redirects in Rails, new to 2.0 we have a few more methods available to us.

1
# prior method<br>headers["Status"] = "301 Moved Permanently"<br>redirect_to "/"<br><br># condo has been defined as a resource in my route.rb<br>redirect_to condo_url(@condo), :status =&gt; 301<br>redirect_to condo_url(@condo), :status=&gt; :moved_permanently<br>

{ Comments }

Minimum, maximum, high, low values in Rails

February 8, 2008

No Gravatar

Have an array that needs sorting? Perhaps you have a range of condominiums and you want to know the highest and lowest priced properties? Check it.

1
2
3
4
5
6
7
8
# here's my array
@condo_prices = [1000, 75, 2000, 10000]
 
# returns 75
@condo_prices.min
 
# returns 10000
@condo_prices.max

{ Comments }

Rails 301 redirects

February 7, 2008

No Gravatar

301 redirects are one of the best ways to inform search engines that your URLs have changed. They’re simple to implement, so you should have no reason not to use them.

1
headers["Status"] = "301 Moved Permanently"<br>redirect_to "/"<br>

Done.

{ Comments }

Rails Sendmail and SPAM Filters

February 6, 2008

No Gravatar

One of the sites we’re working on sends an email upon the completion of a registration form. We were having problems with different SPAM filters catching and misflagging our emails. After doing a little research, it looks like we were omitting some important fields. Take a look at the before and after implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# before
# note the lack of headers, sent on, and content type declarations
class Postoffice < ActionMailer::Base
 
  def welcome(email)
    @recipients = email
    @from = "Meeta Support <happy2help@meeta.com>"
    @subject = "Welcome to Meeta.com"
    body[:email] = email
  end
 
  def contact(params)
  end
 
end
 
 
# after
class Postoffice < ActionMailer::Base
 
protected
 
  def welcome(email)
    @recipients = email
    @from         = "Meeta Support <happy2help@meeta.com>"
    headers         "Reply-to" => "happy2help@meeta.com"
    @subject      = "Welcome to Meeta.com"
    @sent_on      = Time.now
    @content_type = "text/html"
    body[:email]  = email
  end
 
end

{ Comments }

Valid email? Validate email addresses in Rails!

February 5, 2008

No Gravatar

Let’s face it, most of us creating apps collect at a very minimum a name and email address. By a simple validation method to our model, we can easily check the length, format, and presence of our fields. Checking the format of an email address is also simple, but requires a little more work. Using validates_format_of we are able to validate an email address against a regular expression (RegEx).

1
2
3
4
5
# our User model
class User < ActiveRecord::Base
  validates_presence_of :email
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
end

Read up on validations at the Rails API
Read up on regular expressions

{ Comments }