LoginSignup
13
12

More than 5 years have passed since last update.

モジュール内に入れたモデルの階層構造にコントローラーのパスが引きずられないようにする

Last updated at Posted at 2013-05-29

モジュールの特異メソッドとして use_relative_model_naming? を定義して true を返せばいいようです。

デフォルトだと

module Foo
  class User < ActiveRecord::Base
  end
end

Foo::User.model_name.singular_route_key # => "foo_user"
Foo::User.model_name.route_key          # => "foo_users"
Foo::User.model_name.param_key          # => "foo_user"
polymorphic_path(Foo::User.create)      # => "/foo/users/1"
polymorphic_path(Foo::User.new)         # => "/foo/users"

use_relative_model_naming? を定義しておくと

module Foo
  def self.use_relative_model_naming?
    true
  end
  class User < ActiveRecord::Base
  end
end

Foo::User.model_name.singular_route_key # => "user"
Foo::User.model_name.route_key          # => "users"
Foo::User.model_name.param_key          # => "user"
polymorphic_path(Foo::User.create)      # => "/users/1"
polymorphic_path(Foo::User.new)         # => "/users"
13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12