Forum: Ruby on Rails redirect_to and route mapper question

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.
jack.tang (Guest)
on 2007-02-10 05:12
(Received via mailing list)
Hi

I have a question about route mapper and redirect_to. In routes.rb,
there are two url mappings

map.connect  'app/person/:person_name', :controller => 'app', :action
=> 'person_profile'
map.connect  'app/person/:person_name/book/:book_id', :controller =>
'app', :action => 'personal_reading'

In the controller, i can redirect to the first mapping using

redirect_to :action=> "person_profile", :id=>@personal_name

But I don't know how to redirect to the 2nd mapping. Could you please
tell me how to solve it? or tell me how to design url structure in
RoR? thanks!

--jack
Alex W. (Guest)
on 2007-02-10 09:18
jack.tang wrote:
> Hi
>
> I have a question about route mapper and redirect_to. In routes.rb,
> there are two url mappings
>
> map.connect  'app/person/:person_name', :controller => 'app', :action
> => 'person_profile'
> map.connect  'app/person/:person_name/book/:book_id', :controller =>
> 'app', :action => 'personal_reading'
>
> In the controller, i can redirect to the first mapping using
>
> redirect_to :action=> "person_profile", :id=>@personal_name
>
> But I don't know how to redirect to the 2nd mapping. Could you please
> tell me how to solve it? or tell me how to design url structure in
> RoR? thanks!
>
> --jack

Try:

  redirect_to :controller => 'app',
              :action => 'person_profile',
              :person_name => @personal name

And:

  redirect_to :controller => 'app',
              :action => 'personal_reading',
              :person_name => @personal_name,
              :book_id => @book.id

But this can be easier!  Change your routes to:

  map.person 'app/person/:person_name',
             :controller => 'app',
             :action => 'person_profile'

  map.reading  'app/person/:person_name/book/:book_id',
               :controller => 'app',
               :action => 'personal_reading'

Now you can do:

  redirect_to person_url(:person_name => @personal_name)
  redirect_to reading_url(:person_name => @personal_name, :book_id =>
@book.id)

Aint that snazzy
This topic is locked and can not be replied to.