Recent Posts - Page 31

Rendering a ERb template stored in the database to a PDF

June 04, 2005

In my PDF generation project, I would like the PDF templates to be stored in the database rather than on the file system. (not everyone who admins the system should have access to the file system).

So, what I did, after playing with all the different render_* methods in Action Controller, was create a new method called render_string_to_string. This marks my first attempt to submit a patch. After doing so, I noticed that Rails trunk included a completely refactored version of action_controller.rb. Perhaps this functionality exists in trunk now, I haven’t had a chance to play with it yet.

However, in the meantime, I can do this:

    letter_template = LetterTemplate.find_by_id(1)

    foo = render_string_to_string(letter_template.body)

    pdf=FPDF.new
    pdf.AddPage
    pdf.SetFont('Times')
    pdf.SetFontSize(12)

    pdf.Write(5, foo)

    pdf.Output

This is the first time that I have started to tinker (all 4 lines) with the actual Rails code base. I’ve wandered around it, but have stuck to using Rails rather than trying to modify it.

If there is a better way to handle this, please let me know. :-)

RubyURL-friendly library

June 01, 2005

Vincent Foley-Bourgon has just released ShortURL.

From his post:

``` You can install it with RubyGems:

$ gem install -r shorturl

or you can go to http://rubyforge.org/frs/?group_id=732 and download the tar.bz2 archive.

Usage is very simple, here’s a sample irb session:

require “rubygems” => true require “shorturl” => false ShortURL.shorten(“http://groups-beta.google.com/group/comp.lang.ruby”) => “http://rubyurl.com/eiRDm” ShortURL.shorten(“http://groups-beta.google.com/group/comp.lang.ruby”, :tinyurl) => “http://tinyurl.com/bfuu2” ```

On that note, RubyURL is almost at 1,000 unique URLs!

Parsing a RSS Feed

May 11, 2005

A friend was asking me how they could easily read a RSS feed and display the last x items in their rails project. This was my quick and dirty response.

require 'rss/2.0'
require 'open-uri'

class RssfeedController < ApplicationController

  def index
    feed_url = 'http://www.planetrubyonrails.org/xml/rss'
    output = "<h1>My RSS Reader</h1>" 
    open(feed_url) do |http|
      response = http.read
      result = RSS::Parser.parse(response, false)
      output += "Feed Title: #{result.channel.title}<br />" 
      result.items.each_with_index do |item, i|
        output += "#{i+1}. #{item.title}<br />" if i < 10  
      end  
    end
    render_text output
  end

end

Is there an easier way to do this with another RSS library? I figured that the simplest method would be to just use the standard library that comes with Ruby.

Thanks for the kind words

April 27, 2005

Now that I have signed myself over to O’Reilly, I am looking at my deadlines and feeling pretty energetic about the whole thing. Over the past 30 hours or so, I have received loads of emails, comments, and blog posts in regards to my book signing. I wanted to let you all know how much I appreciate it. Your support has definitely given me even more energy to put into this project.

In regards to how I will blog my writing, I will probably not discuss too many of the details of the work that I am working on, but I plan to continue writing tutorials and overviews of things that I find of interest in regards to Ruby on Rails.

Luckily, my employer (being your own boss rules!) is kind enough to allow me to devote a lot of my time over the coming months to write this book. :-)

Again, thanks for the positive feedback!

RubyURL.com in a hour...

March 14, 2005

Ok, I will admit it. I spent much longer than a hour on this. I spent about a hour coding, testing and adding a few CSS tags to the few pages. I spent a few hours trying to figure out why my RewriteRule was not working.

At about 10pm on Sunday evening, I had an idea… hey, I’ll spend the rest of the night on a simple Rails project… and after being sent a link for a tinyurl, I thought, Hey, I’ll just create RubyURL.com. So, I checked godaddy.com, the domain was available and I will now own it until this time next year.

The system thus far comprises of 1 database table, 2 controllers and 1 model that were added to the default rails install. (not including the Rails templates for new.rhtml and show.rhtml).

The random character string function that I used is here: This allos me to create some random alpha-numeric string like ur029

class Rubyurl < ActiveRecord::Base

  def self.gen_random(size=5)
    chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" 
    short_url = "" 
    srand
    size.times do
      pos = rand(chars.length)
      short_url += chars[pos..pos]
    end
    short_url
  end

end

So, with the standard usage of scaffold I was able to create a random string prior to saving my form:

def create
    @rubyurl = Rubyurl.new(@params['rubyurl'])
    @rubyurl['short_url'] = Rubyurl.gen_random(5)
    if @rubyurl.save
      flash['notice'] = 'Rubyurl was successfully created.'
      redirect_to :action => 'show', :id => @rubyurl.id
    else
      render_action 'new'
    end
  end

The last part in the ruby code was to make it actually redirect to another location:

class GoController < ApplicationController

  scaffold :rubyurl

  def go
  end

  def index
    @url = Rubyurl.find_first(["short_url = ?", @params["id"]])
    if @url['website_url']
      redirect_to @url['website_url']
    end
  end

end

… so just over 2 1/2 hours later (mainly due to a problem with the RewriteRule..), I am now happy to announce the alpha release of RubyURL.

I’ll write more about my findings when I wake up.. off to bed.

BloGTK and Typo

March 13, 2005

When you run BloGTK and Typo together, make sure you set the API to MoveableType in your Preferences.

..and a screenshot of me posting this entry.