(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 ?
Something, like this:
and get in your controller
or without year
so, you can use in your controller
And url_helper