Hi all
I am having some unexpected results with the link_to tag, and would
appreciate any ideas as to why it is doing what it is or alternately
what I am doing wrong.
I have a site with a clear hierarchy. Each level has a one to many
relationship with the level below it:
categories->subcategories->levels->products.
For the admin section one is able to edit categories and next to each
category is a link to list the subcategories. Clicking on that lists
all the subcategories for that category, and so on, down to products.
The route for this is
map.connect ''/:controller/:cat/:sub/:lev/:action/:id'',
:controller =>
''admin'', :cat => nil, :sub => nil, :lev => nil,
:action => ''index'',
:id => nil, :requirements => {:cat => /\d/, :sub => /\d/, :lev =>
/\d/, :id => /\d/}
Now, when I for example want to link to all the subcategories for a
category I put in
<%= link_to ''Subcategories'', :controller =>
''admin'', :cat => category %>
expecting a link such as http://localhost:3000/admin/4
but instead wind up with links like http://localhost:3000/admin?cat=4
and even worse with
<%= link_to ''Products'', :cat => @params[:cat], :sub
=> @params[:sub],
:lev => level %>
I get http://localhost:3000/admin?lev=1&sub=1&cat=1
instead of http://localhost:3000/admin/1/1/1
If I remove the :requirements part of my route it displays the link
correctly, however it then with a link such as /admin/new tries to
display all subcategories where category_id is ''new'' as it
obviously
does not know that id, cat, etc should be digits.
I have also tried to do this with a separate route for each of the
four levels of navigation (ie without cat, sub and lev, with only cat,
with cat and sub, etc), but this hasn''t helped at all.
Additionally, I have tried something along the lines of
<%= link_to ''Products'', :cat => @params[:cat].to_i, :sub
=>
@params[:sub].to_i, :lev => level %>
ie using to_i as I thought perhaps the parameters were not meeting the
requirements part of the route hash as they were strings and not
integers but this has also not helped.
I have read everything I can on find on routes but have not found an
answer. I am rather new to rails, ruby and pretty everything I am
doing here, so it''s quite possible that I am overlooking something
obvious, so if I am please do forgive me =)
Thanks in advance
Luke