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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:46:28+00:00 2026-05-31T12:46:28+00:00

(Note that the examples are a simplification of my use case.) I have a

  • 0

(Note that the examples are a simplification of my use case.)

I have a Model Event and the default route for showing one event:

/events/:id(.:format) ----> activities#show

Now, the event has an attribute named year and I am also already accepting the following route:

/events/:year/:id(.:format) ----> activities#show

As the second variant shall be the default, I want code like redirect_to @event to lead the user to that variant (e.g. http://localhost:3000/events/2012/1).

Where do I define that?

So far, I’ve tried to overwrite to_param in the model like so:

def to_param
  "#{self.year}/#{self.id}"
end

… but that will cause params[:id] to be filled with the string 2012/1 .

I want params[:id] to still be 1 while params[:year] should be 2012.

Does Rails provide a standard way to accomplish this?

… or will I have to write a custom method for the model and call it by issuing something like this: redirect_to @event.custom_method_to_create_long_url_variant

Such a method would be quickly implemented, but if Rails can handle the requirement via a simple setting, it would allow me to leave pretty much all other code untouched.

EDIT:

I found out that my params held the values (:id and :year) as expected when changing the get in the functional test FROM get :show, id: @event TO get :show, id: @event.id.

Now the question is:

Is that just the way to do it or can I still support get :show, id: @event ?

  • 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-05-31T12:46:30+00:00Added an answer on May 31, 2026 at 12:46 pm

    Something, like this:

    match "/events(/:year)/:id(.:format) => "activities#show"
    

    and get in your controller

    Started GET "/events/2012/12" for 127.0.0.1 at 2012-03-14 17:25:44 +0400
    Parameters: {"year"=>"2012", "id"=>"12"}
    

    or without year

    Started GET "/events/12" for 127.0.0.1 at 2012-03-14 17:25:44 +0400
    Parameters: {"id"=>"12"}
    

    so, you can use in your controller

    params[:id]
    params[:year]
    

    And url_helper

    match "/events(/:year)/:id(.:format) => "activities#show", :as => :my_event
    
    my_event_path(2012, 12) #=> /eventes/2012/12
    my_event_path(@event.id) #=> /events/12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note that this function does not have a { and } body. Just a
(Please note that I have seen a similar question on StackOverflow recently, however I
Note that the following are examples of the rare cases where dotNet reflector does
In most examples of WCF services that use MSMQ transport it is always preferred
Has anyone seen this type of IE display problem? Example http://xs133.xs.to/xs133/08465/ie_problem910.jpg.xs.jpg Note that it
Note that I am not asking which to choose (MVC or MVP), but rather
Note that I'm not talking about the compiler-generated Temporary ASP.NET Files. My web application
Note that the _src inherit IQueryable<U> and V inherit new() ; I wrote the
(note that this question is not about CAS, it's about the May fail spuriously
Note that I'm aware of other yield in vb.net questions here on SO. I'm

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.