Re: Query sensible URL from form

Alex Young
Wed, 31 Aug 2005 09:02:33 -0000

Florian Ebeling wrote:
> Consider a small form:
>
> User: [________]  [Submit]
>
> Then you want to go to a sensible ("pretty") URL like
>
> /messages/user/febeling
>
> Is there a rails approach to this? Or is it dumb to aspire?
> The GET would obviously be cluttered with lots of ?, = and &,
> while a POST is completely silent.

Looks like you either want a javascript rewrite on the client side
(which I''m not sure could degrade well), or a server-side redirect.
The
redirect would give you a chance to punt to an error page if you needed,
too.  I guess the action would look something like this:

def user_lookup
  begin
    user = User.find_by_name(@params[''name''])
    redirect_to :controller=>''messages'',
:action=>''user'', :id=>user.name
  rescue RecordNotFound
    redirect_to :controller=>''messages'',
:action=>''error''
  end
end

Untested, and you''ll probably want to do something with the route, but
that looks about right to me.  I''m sure someone eminently more
qualified
will comment shortly...

--
Alex