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 8503041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:39:00+00:00 2026-06-11T01:39:00+00:00

I have a model Image: class Image < ActiveRecord::Base attr_accessible :description, :name, :size, :image,

  • 0

I have a model Image:

class Image < ActiveRecord::Base
  attr_accessible :description, :name, :size, :image, :tag_ids

  has_many :taggings, :dependent => :destroy
  has_many :tags, :through => :taggings
end

Then I have my Tag model:

class Tag < ActiveRecord::Base
  attr_accessible :name
  has_many :taggings, :dependent => :destroy
  has_many :images, :through => :taggings

end

My routes.rb is currently:

resources :images do
    get 'confirm_destroy', :on => :member
end
resources :tags

Now let’s say I created a few tags “blue”, “red” and “yellow” for the images. On some page I want to show a list of tags and then link them to e.g. http://www.example.com/yellow where all the images tagged as yellow shall be shown. The view (haml) for this tag list is currently:

- @tags.each do |tag|
  = link_to(tag.name, tag)

But it generates a link to http://www.example.com/tags/2 (with 2 being the tag_id).

How can I create the correct resources in order to link to http://www.example.com/yellow instead of http://www.example.com/tags/2. Will the view with “link_to” be the same in this case?

  • 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-11T01:39:01+00:00Added an answer on June 11, 2026 at 1:39 am

    You won’t be able to create a route to http://www.example.com/yellow because this doesn’t reference a specific resource, and subsequently could create conflicts. Imagine if you had a tag called ‘images’, Rails wouldn’t know if the url to http://www.example.com/images referred to a specific tag, or the images resource.

    The best we can do is create a resource which uses the name as the identifier in the URL, such that http://www.example.com/tags/yellow would show the tag with ‘yellow’ as its name attribute.

    To do this, you need to define the following to_param method in the model for Tag.

    class Tag < ActiveRecord::Base
      attr_accessible :name
      has_many :taggings, :dependent => :destroy
      has_many :images, :through => :taggings
    
        def to_param
            name
        end
    end
    

    This will tell Rails to use the name attribute for routing instead of the default id. Your link_to won’t need to be updated, however, your Tag controller will need to now find the Tag by name as opposed to ID, like so:

    class TagsController < ApplicationController
    
        def show
            @tag = Tag.find_by_name(params[:id])
        end
    
        ...
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Pin Model: class Pin < ActiveRecord::Base belongs_to :user belongs_to :image accepts_nested_attributes_for
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have 2 models (Book & Image) class Book < ActiveRecord::Base has_many :images accepts_nested_attributes_for
I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable
I have 3 models: products, tags and products_tags: Tag model: class Tag < ActiveRecord::Base
I have a model Image and every image is having description. User can add
(I am new to paperclip) I have this model hierarchy: base model: class QuestDescription
Say I have the following models: class Image(models.Model): image = models.ImageField(max_length=200, upload_to=file_home) content_type =
I have two models nested with accepts_nested_attributes_for : class Place < ActiveRecord::Base # Relations..
For example there are some models class Model_1 < ActiveRecord::Base has_many :images, :as =>

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.