fail_lord fail_lord - 4 months ago 10
Ruby Question

Rails resources and custom urls

I have a model Posts with titles, categories, months, and tags. When I filter these using a parameter, how do I construct the routes to look like this:

/posts/title-of-post

/categories/category-of-post

/months/month-year

/tags/name-of-tag


Currently, the URL looks like
/?utf8=✓&category=dingus
. I understand the solution has something to do with resources buts it's very confusing. Thanks.

Answer

This won't be a best practice answer, but it looks like you've already went the other way, so

In you routes.rb create a route:

get 'categories', to: 'posts#categories', as: 'show_categories'

In your Posts controller you can create a new action called categories:

def categories
  @posts = Post.where ... (pick your catgory or month using params)
end

Create a new categories.html.erb view in your Posts view folder, and there you go.