Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:59:09+00:00 2026-06-12T13:59:09+00:00

i am returning to an application I wrote a few months ago and trying

  • 0

i am returning to an application I wrote a few months ago and trying to make a few minor adjustments. I currently have a model Product and a catalogue page where all products are listed using an each loop for all entries in Product.

In order to attempt to keep the site secure I have put http://.../products/... behind a authentication login, so only users with a password can see /products/index, edit the product details and also see the products one at a time (show page).

I would now like to link from the page with all products listed to a page with just ONE product’s details (i.e. an individual product’s own page).

I am aware that I can find a product from the Product model using a find function for a Product ID, however, how do I go about automatically creating a sensible URL for this product’s listing?

In summary, if I had a listing of 10 products on a page, what is the best way of enabling a user to click on the product and be routed to a page called http://..../shop/specific_products_name? I have read around a bit and it appears to_param may be of some benefit, but I am struggling to work out a sensible means of creating these pages

Thanks for your time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:59:11+00:00Added an answer on June 12, 2026 at 1:59 pm

    Here’s a basic, conventional controller setup:

    First, here’s the route shortcut syntax:

    # config/routes.rb
    ...
    resources :products
    ...
    

    Here’s the controller that loads all the Products on the index and just one Product on the show.

    # app/controllers/products_controller.rb
    class ProductsController 
      def index
        @products = Product.all
      end
    
      def show
        @product = Product.find(params[:id])
      end   
    end
    

    That line in the routes file gives us these routes (among others):

    • /products goes to products_controller#index
    • /products/:id goes to products_controller#show with params[:id] set to the given number

    The easiest way to get a pretty URL with the least amount of work is to, as you looked into, override to_param.

    to_param is what Rails uses to turn a model into a representation in a url/request.

    By default, it’s this:

    class Product
      def to_param
        self.id
      end
    end
    

    So when Rails does a Product.find() on the result of that to_param, find() turns it into an integer with to_i.

    If params[:id] is "24", then Product.find(params[:id]) converts “24” to the integer 24.

    But consider how to_i works with other examples:

    "100".to_i                     #=> 100
    "100-flux-capacitor-2000".to_i #=> 100
    

    In other words, if our parameter representation of a Product is "#{id}-the-product-name", Product.find(params[:id]) will still work even though our products#show URLs look like /products/56-ford-taurus.

    One more thing:

    "I love the Ford Taurus".parameterize #=> "i-love-the-ford-taurus"
    

    Solution A

    The easiest solution to your problem is to override to_param like this:

    class Product
      def to_param
        "#{id}-#{name.parameterize}"
      end
    end
    

    The benefit of this method is that you change one method in the Product model and the rest of your code Just Works.

    The downside is that your URLs looks like:

    /products/56-ford-taurus
    

    Instead of:

    /products/ford-taurus
    

    Solution B

    If you want the latter example, you can maintain a slug column in the Products table.

    Example:

    Product
      id: 58
      name: Ford Taurus
      slug: ford-taurus
    

    Then you could override to_param like this:

    class Product
      def to_param
        slug
      end
    end
    

    Now, params[:id] would look like “ford-taurus”.

    Product.find("ford-taurus") wouldn’t work anymore since find() wants an integer, so you could replace Product.find(params[:id]) with Product.find_by_slug(params[:id]).

    You’d just have to make sure every product has a unique slug.

    And you can automate the slug creation process with something like:

    class Product
      before_create :make_slug
    
      private
      def make_slug
        self.slug = name.parameterize
      end
    end
    

    That way, you just need to set the product’s name on creation, save it, and its slug will become a URL friendly “whatever-the-product-name-was”.

    • Plus side: Your URLs now only contain the product name with no ugly product ID.
    • Down side: Changing the slug a product will break URLs, which is why my before_filter example only sets the slug on creation and not any time you save the product.

    Now that Rails will generate the kind of URLs you want, you can generate links to these products in a view like this:

    # This would be in views/index.html.erb
    <% @products.each do |product| %>
      <%= link_to product.name, product %>
    <% end %>
    

    Would product this sort of html for each product:

    <a href="/products/56-ford-taurus">Ford Taurus</a> (if you used Solution A)
    <a href="/products/ford-taurus">Ford Taurus</a> (if you used Solution B)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a similar problem a few months ago, but that was easily solved
I wrote an ASP.NET web application. My application created a request with returning URL
I have a situation where I'm returning json objects to my application which are
Am I breaking any laws in the REST bible by returning application/octet-stream for my
I'm trying to use a timer to schedule a recurring event in an application.
I am returning a model from my controller that displays a grid. Works fine.
I have a service returning an array of type BaseItem. BaseItem has N subtypes.
I am building an Android application and I have chosen to use flat flies
I trying to write an application that has a collection of students using different
I have a web application that creates XML feeds on the fly, depending on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.