How to create Urls => Controller


#1

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 :slight_smile:

Regards,
Jamal


#2

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


#3

Okay, thats real awesome :smiley:

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

Amazing framework :smiley:


#4

Jamal S. wrote:

Okay, thats real awesome :smiley:

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

Amazing framework :smiley:

And of course “Thanks For Your Help Jeff C.”