【问题标题】:Rails 3 routes and modulesRails 3 路线和模块
【发布时间】:2010-06-17 14:44:21
【问题描述】:

我在模块中有一个 AR 模型

class Long::Module::Path::Model < ActiveRecord::Base
end

并且想使用以下路由(没有模块名称,因为它更容易编写和记忆)

resources :models

但 Rails 3 总是想使用类似的 URL

long_module_path_model_url

有没有办法改变这种行为?

希望有人可以帮助我吗?

马里奥

【问题讨论】:

    标签: routing routes ruby-on-rails-3


    【解决方案1】:

    我有点好奇为什么您在谈论仅处理控制器级别的路由时要引用模型;但这篇文章应该会有所帮助:R3 Controller Namespaces and Routing

    "如果你想将 /photos(不带前缀 /admin)路由到 Admin::PostsController,你可以使用:

    scope :module => "admin" do
      resources :posts, :comments
    end
    

    "

    如果您想更改命名路径,可以使用 :as,此处指定:R3 Prefixing the Named Routes Helpers

    所以我猜测是这样的

    1:

    scope :module => 'long/module/path' do
       resources :model, :as => :model
    end
    

    或 2:

    scope :module => 'long' do
      scope :module => 'module' do 
       scope :module => 'path' do
        resources :model, :as => :model
       end end end
    

    就是你要找的东西。

    【讨论】:

    • 不,你不理解我 ;) 我想使用 model_path(@model) 访问我的模型,而不是 long_module_path_model(@model)。似乎没有工作。
    • 或许上面的修改版更好。
    【解决方案2】:
    resources :your_looooooong_model_name, :as => :short
    

    会给你shorts_url等

    【讨论】:

    • 不,抱歉没有为我工作...现在有很多路由错误。
    【解决方案3】:

    我知道这是一个老问题,但其他人误解了你的问题,并没有解决你的问题。

    您需要重写 model_name 方法,如下所示:

    class Long::Module::Path::Model < ActiveRecord::Base
      def self.model_name
        ActiveModel::Name.new(Long::Module::Path::Model, nil, "YourNewModelName")
      end
    end
    

    归功于comment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多