Hi , do we have to use controller name in url , can we change this? For example i have a controller named => category when user click to link "Computer Components" can rails write http://domain/computer-components instead of http://domain/categories/53
map.computer_components ''computer_components'',:controller => "categories",:action => "your_action_name'' Now helper will be computer_components_url Sijo -- Posted via http://www.ruby-forum.com/.
On 19 June, 11:48, lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi , do we have to use controller name in url , can we change this? > For example i have a controller named => category > when user click to link "Computer Components" > can rails writehttp://domain/computer-componentsinstead ofhttp://domain/categories/53Hi, This is possible by creating a custom route in routes.rb. You would need to do the following: map.connect ''/:category_name'', :controller => ''categories'', :action => ''show'' However, you need to be careful where this sits in your routes.rb as it will catch every URL if it is too high in the file. In your categories controller, the show action would need the following: @category = Category.find_by_name(params[:category_name] I should point out that this is not good practice and if you intend to have a significant number of categories, the id should be used. The following link shows how you can get ''friendly'' urls whilst still using standard Rails routing. http://www.jroller.com/obie/entry/seo_optimization_of_urls_in Regards Robin
Many thanks Robin , yes it''s possible doing this as you said but i have too much categories and subcategories (using acts_as_tree) , Method i will use must be dynamic , if not i cant handle with 100categories On Jun 19, 2:44 pm, Robin Fisher <robinjfis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 19 June, 11:48, lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi , do we have to use controller name in url , can we change this? > > For example i have a controller named => category > > when user click to link "Computer Components" > > can rails writehttp://domain/computer-componentsinsteadofhttp://domain/categories/53 > > Hi, > > This is possible by creating a custom route in routes.rb. You would > need to do the following: > > map.connect ''/:category_name'', :controller => ''categories'', :action => > ''show'' > > However, you need to be careful where this sits in your routes.rb as > it will catch every URL if it is too high in the file. In your > categories controller, the show action would need the following: > > @category = Category.find_by_name(params[:category_name] > > I should point out that this is not good practice and if you intend to > have a significant number of categories, the id should be used. The > following link shows how you can get ''friendly'' urls whilst still > using standard Rails routing. > > http://www.jroller.com/obie/entry/seo_optimization_of_urls_in > > Regards > > Robin