Generate unescaped HTML with Rails 3

Posted by Dan Sosedoff on November 24, 2010

Since i switched to Rails 3 i figured out that you dont need to escape html contents (using h method in Rails 2), its been done automatically. So, for now if you have any plugins or methods that produce raw html you have to use method “html_safe” to unescape it.

Before:

<%= your_helper_method %>

Now:

# your helper method
def your_helper_method
    # .... content generation
    return content.html_safe
end

In in view:

<%= your_helper_method %>

Using Authlogic with Rails 3

Posted by Dan Sosedoff on November 15, 2010

Authlogic is a good authentication library for Rails, supports a lot of different methods. But its not fully compatible with Rails 3 which is solvable. This solution works for latest gem version (2.1.6):

Basically, default setup is the same, nothing specific;

class UserSession < Authlogic::Session::Base ; end

And you’ll probably get error message like this:

undefined method `to_key’ for <UserSession: no credentials provided>

In this case all you have to do is to define additional method in your UserSession model:

class UserSession < Authlogic::Session::Base
  def to_key
    new_record? ? nil : [ self.send(self.class.primary_key) ]
  end
end

And everything should work just fine.

Debugging email output in Rails

Posted by Dan Sosedoff on November 05, 2010

Need to troubleshoot email output? No problem. Just need to configure your development environment with the following lines:

# RAILS_ROOT/config/environments/development.rb
config.action_mailer.delivery_method = :sendmail 
config.action_mailer.sendmail_settings = {:location => "/usr/bin/fake-sendmail.sh"}
config.action_mailer.default_url_options = { :host => "YOUR HOST" }

And here is the fake-sendmail utility i wrote: (save as “/usr/bin/fake-sendmail.sh”)

#!/usr/bin/ruby
 
dir = "/tmp/fake-sendmail"
headers = ''; $stdin.each_line { |l| headers << l ; break if l.strip.length == 0 }
body = '' ; $stdin.each_line { |l| body << l }
format = body.match(/html/i) ? "html" : "txt"
filename = Time.now.to_i
 
Dir.mkdir(dir) unless File.exists?(dir)
File.open("#{dir}/#{filename}_headers.txt", "w") { |f| f.write(headers) }
File.open("#{dir}/#{filename}.#{format}", "w") { |f| f.write(body) }

Paste: http://pastie.org/private/er61gafxb6fzowjo4chjvq

As the result you’ll see 2 files created for each outgoing email. One has all email headers and another one has the body.

Also, it works for Sinatra and Merb. Woot!

Custom SQL queries in Rails

Posted by Dan Sosedoff on August 17, 2010

Sometimes ORM just cant hand some complicated query, especially when the results of such query does not refer to any table.

For Rails ActiveRecord:

class Item < ActiveRecord::Base
   def self.some_heavy_stuff(param)
     sql = self.sanitize_sql(['.... SQL ....', param)
     self.connection.execute(sql)
   end
end
 
# usage
data = Item.some_heavy_stuff(1)
data.each do |row|
   puts row['id']
end

For DataMapper its almost the same (they`ve replaced query method with select not a long time ago):

class Item
  include DataMapper::Resource
 
  property :id, Serial
  property :foo, String
  property :bar, String
 
  def self.some_heavy_stuff(param)
     repository(:default).adapter.select('... SQL ...')
  end
end