Where does this magical path come from?
It took me a lot of tracing, but in the end I found that url_for defines the path for your model using the polymorphic_path
method defined in ActionDispatch :: Routing :: PolymorphicRoutes . polymorphic_path
ultimately gets the auto-path for your model, calling something line by line:
record.class.model_name.route_key
I simplify a bit, but that is basically it. If you have an array (for example, form_for[@order, @item]
), then it is called above for each element, and the results are combined with _
.
The model_name
method in your class comes from ActiveRecord :: Naming .
module ActiveModel ... module Naming ... def model_name @_model_name ||= begin namespace = self.parents.detect do |n| n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming? end ActiveModel::Name.new(self, namespace) end end end end
How to change it?
Fortunately, ActiveModel :: Name pre-computes all values, including route_key, so to redefine this value all we need to do is change the value of the instance variable.
For the resource :order
in your question:
class Order < ActiveRecord::Base model_name.instance_variable_set(:@route_key, 'order') ... end
Give it a try!
jshkol Apr 26 '13 at 5:09 on 2013-04-26 05:09
source share