From 2ea95a522a058183c27552d78722ed52716b3b13 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 1 Aug 2015 23:08:53 +0900 Subject: [ci skip] Add descriptions about `ActiveRecord::Base#to_param` to * `ActionDispatch::Routing::Base#match` * Overriding Named Route Parameters (guide) When passes `:param` to route definision, always `to_param` method of related model is overridden to constructe an URL by passing these model instance to named_helper. --- guides/source/routing.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 52f11f92bd..02763eabcd 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -1087,6 +1087,20 @@ edit_videos GET /videos/:identifier/edit(.:format) videos#edit Video.find_by(identifier: params[:identifier]) ``` +You can override `ActiveRecord::Base#to_param` of a related +model to constructe an URL. + +```ruby +class Video < ActiveRecord::Base + def to_param # overridden + identifier + end +end + +video = Video.find_by(identifier: "Roman-Holiday") +edit_videos_path(video) # => "/videos/Roman-Holiday" +``` + Inspecting and Testing Routes ----------------------------- -- cgit v1.2.3