Created
July 7, 2009 00:09
-
-
Save tonywok/141783 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ShortUrlsController < ActionController::Base | |
# before_filter :login_required | |
def index | |
@urls = Url.paginate :page => params[:page], :per_page => 4, :order => 'updated_at DESC' | |
end | |
def create_link | |
@url = Url.new(:long_url => params['url']) | |
@url.save! | |
@url = Url.find_by_long_url(@url.long_url) | |
# render :text => 'http://localhost:3000/' + @url.short_url | |
#render :template => 'short_urls/index' | |
end | |
def redirect_to_original | |
@url = Url.find_by_id(from_64(params['short_url'])) | |
redirect_to @url.long_url | |
end | |
def test_url_shortener | |
render :template => 'short_url/test_form' | |
end | |
private | |
def from_64(s) | |
tot = 0 | |
s.split('').reverse.each_with_index do |c, place| | |
n = Url::VALID_BASE64_CHARS.index(c) | |
tot += (n * (64 ** place)) | |
end | |
tot | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Url Listing</h1> | |
<table padding = 2px> | |
<tr> | |
<td><center><b>long url</b></center></td> | |
<td><center><b>short url</b></center></td> | |
</tr> | |
<% for url in @urls %> | |
<tr> | |
<td align = "center"> | |
<a href = "<%=h url.long_url %>"> <%=h url.long_url %> </a> | |
</td> | |
<td align = "center"> | |
<a href = "http://localhost:3000/<%=h url.short_url %>"> <%=h url.short_url %> </a> | |
</td> | |
</tr> | |
<% end %> | |
<tr> | |
<td colspan = '2', align = "center"><%= will_paginate @urls %></td> | |
</tr> | |
</table> | |
<br /> | |
<br /> | |
<%= link_to 'New Url to be Shortened', new_short_url_path %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment