skip to Main Content

I feel this is should be easy but I’m stuck. What’s the best way to map a route and have it point to specific url?

User types/click http://acne.com/category-name and it will get http://acne.com/?category[]=1

I don’t want to redirect to ?category[]=1 though. I want to make it SEO friendly.

2

Answers


  1. Use the friendly_id gem.

    Here is an example o how it could work:

    class Category < ActiveRecord::Base
      extend FriendlyId
      friendly_id :name
    end
    
    @category = Category.friendly.find "category-name"   
    @category.to_param
    redirect_to @category # the URL will be /category/category-name
    
    Login or Signup to reply.
  2. If I understood your question correctly, you want to show the category when user types in acne.com/category-name.

    Add this in your config/routes.rb:

    get ‘/:category-name’, to: ‘category#show’

    This will route the request to your categoryController’s show method with the user-typed category name in the “category-name” parameter. You can handle what is displayed there (use params[:category-name] to find out the category name the user has typed in)

    Sorry for the lack of code tags, I’m on mobile.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search