I have a product page,I want to change the url "example.com/products/ list?category_id=1&price_id=5&type_id=2&sort_by=name&......" to "example.com/products/list/category_id/1/price_id/5/type_id/2/sort_by/ name.....". my routes like match ''/products/list(/*specs)'', :controller => ''products'', :action => ''list'',:as => :list_products My controller @products=Product.scoped @products = @Product.where(Hash[*params[:specs].split("/")]) if params[:specs] In my views: Categories: <% Category.all.each do |category| %> <p><%= link_to category.name,list_products_path(:specs => [''category'', category]) %></p> <% end %> Price: <% Prices.all.each do |price| %> <p><%= link_to price.name,list_products_path(:specs => [''local'', local]) %></p> <% end %> I wnat to get url like /products/list/category/1/local/2... ,but it can only get url like /products/category/1 or /news/local/2. How to write the link_to method to make it works? Thank you. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Aug-28 00:31 UTC
Re: How to write link_to method to Rails route globbing
On Fri, Aug 26, 2011 at 3:07 AM, doabit <doinsist-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a product page,I want to change the url "example.com/products/ > list?category_id=1&price_id=5&type_id=2&sort_by=name&......" to > > "example.com/products/list/category_id/1/price_id/5/type_id/2/sort_by/ > name.....". > my routes like > > match ''/products/list(/*specs)'', :controller => ''products'', :action > => ''list'',:as => :list_products > My controller > > @products=Product.scoped > @products = @Product.where(Hash[*params[:specs].split("/")]) if > params[:specs] > In my views: > > Categories: > <% Category.all.each do |category| %> > <p><%= link_to category.name,list_products_path(:specs => > [''category'', category]) %></p> > <% end %> > Price: > <% Prices.all.each do |price| %> > <p><%= link_to price.name,list_products_path(:specs => [''local'', > local]) %></p> > <% end %> > I wnat to get url like /products/list/category/1/local/2... ,but it > can only get url like /products/category/1 or /news/local/2. How to > write the link_to method to make it works? Thank you. > >Setup your routes using nested resources. http://guides.rubyonrails.org/routing.html#nested-resources After setting that up, use rake routes to find the url you need.> -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.