Forum: Ruby on Rails How to create Urls => Controller

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.
Jamal S. (Guest)
on 2007-03-06 20:09
Hello there,

I'm doing something like "categories" page, I want to do the URLs like
this.

http://domain/category/ [show everything in all categories]
http://domain/category/movies [show only in movies category]
http://domain/category/music [etc]

In PHP I used .htaccess to do that, but I wonder how to do this in RoR,
should I also use .htaccess, or I can use the category controller to
handle all the actions in one method (index) ?

Thanks for any help :)

Regards,
Jamal
Jeff C. (Guest)
on 2007-03-06 20:52
(Received via mailing list)
On Mar 6, 12:09 pm, Jamal S. <removed_email_address@domain.invalid>
wrote:
> should I also use .htaccess, or I can use the category controller to
> handle all the actions in one method (index) ?

In Rails you use the routes.rb file to map incoming URLs to your
controllers and actions.

map.connect '/category/:filter/, :controller => 'categories', :action
=> 'index'

In your CategoriesController class, you can now inspect
params[:filter] to see what (if anything) was specified:

class CatagoriesController < ApplicationController

  def index
    if params[:filter]
      @items = Item.find_all_by_type(params[:filter)]
    else
      @items = Item.find :all
    end
  end

end

and then your index.rhtml can display the @items array.


Jeff
softiesonrails.com
Jamal S. (Guest)
on 2007-03-06 21:00
Okay, thats real awesome :D

Thats why I keep getting more and more in love with RoR :D

Amazing framework :D
Jamal S. (Guest)
on 2007-03-06 21:01
Jamal S. wrote:
> Okay, thats real awesome :D
>
> Thats why I keep getting more and more in love with RoR :D
>
> Amazing framework :D

And of course "Thanks For Your Help Jeff C."
This topic is locked and can not be replied to.