Forum: Ruby on Rails Rails redirect/jump script?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael N. (Guest)
on 2006-03-14 23:11
(Received via mailing list)
I'm looking for some advice on how to write a jump script - something
that will look up a url in my database and redirect a cleaner url to
the one in the database.

For example:
url: http://www.amazon.com
jump url: http://www.mysite.com/jump/a
Pat M. (Guest)
on 2006-03-15 00:17
(Received via mailing list)
On 3/14/06, Michael N. <removed_email_address@domain.invalid> wrote:
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

Should be pretty easy.

class Jump < ActiveRecord::Base
  # name - string
  # url - string
end

class JumpController < ApplicationController
  def jump
    redirect_to_url Jump.find_by_name(params[:id]).url
  end
end

Obviously that's very basic and has no error handling, but you can
just make whatever changes you need.  Then you can do
<%= link_to 'Amazon', :controller => 'jump', :action => 'jump', :id =>
'a' %>

Store some more info like the link name (amazon) and the jump name (a)
in the database if you'd like.  You could throw the jump method in a
main controller or use routes so that the URLs appear as
http://mysite.com/jump/a

Pat
Michael N. (Guest)
on 2006-03-15 07:23
(Received via mailing list)
Thanks, just what I was looking for - didn't know about the
redirect_to_url.
This topic is locked and can not be replied to.