Robby on Rails: RubyURL.com in a hour... thoughts.sort_by{|t| t[:topic]}.collect tag:www.robbyonrails.com,2005:TypoTypo2006-09-05T22:12:46-04:00Robby Russellurn:uuid:8c4ee88b22691e09ddd7e19a07fa6c7f2005-03-14T10:27:51-05:002006-09-05T22:12:46-04:00RubyURL.com in a hour... <p>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 <span class="caps"><span class="caps">CSS</span></span> tags to the few pages. I spent a few hours trying to figure out why my RewriteRule was not working.</p>
<p>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 <a href="http://rubyurl.com/">RubyURL.com</a>. So, I checked godaddy.com, the domain was available and I will now own it until this time next year.</p>
<p>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).</p>
The random character string function that I used is here: This allos me to create some random alpha-numeric string like <b>ur029</b>
<pre>
class Rubyurl &lt; 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
</pre>
So, with the standard usage of scaffold I was able to create a random string prior to saving my form:
<pre>
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 =&gt; 'show', :id =&gt; @rubyurl.id
else
render_action 'new'
end
end
</pre>
<p>The last part in the ruby code was to make it actually redirect to another location:</p>
<pre>
class GoController &lt; 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
</pre>
<p>... 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 <a href="http://rubyurl.com/">RubyURL</a>.</p>
<p>I’ll write more about my findings when I wake up.. off to bed.</p><p>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 <span class="caps"><span class="caps">CSS</span></span> tags to the few pages. I spent a few hours trying to figure out why my RewriteRule was not working.</p>
<p>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 <a href="http://rubyurl.com/">RubyURL.com</a>. So, I checked godaddy.com, the domain was available and I will now own it until this time next year.</p>
<p>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).</p>
The random character string function that I used is here: This allos me to create some random alpha-numeric string like <b>ur029</b>
<pre>
class Rubyurl &lt; 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
</pre>
So, with the standard usage of scaffold I was able to create a random string prior to saving my form:
<pre>
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 =&gt; 'show', :id =&gt; @rubyurl.id
else
render_action 'new'
end
end
</pre>
<p>The last part in the ruby code was to make it actually redirect to another location:</p>
<pre>
class GoController &lt; 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
</pre>
<p>... 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 <a href="http://rubyurl.com/">RubyURL</a>.</p>
<p>I’ll write more about my findings when I wake up.. off to bed.</p>