Rails url_for和命名空间模型 [英] Rails url_for and namespaced models

查看:69
本文介绍了Rails url_for和命名空间模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails中,是否可以在模块中命名空间模型并仍然从url_for获得正确的行为?

In Rails, is it possible to namespace models in modules and still get correct behavior from url_for?

例如,在这里,url_for可以按预期工作:

For instance, here, url_for works as expected:

# app/models/user.rb
class User < ActiveRecord::Base
end

# config/routes.rb
resources :users

# app/views/users/index.html.haml
= url_for(@user)    # /users/1

User模型放入模块后,url_for抱怨未定义的方法m_user_path:

Whereas after putting the User model into a module, url_for complains about an undefined method m_user_path:

# app/models/m/user.rb
module M
  class User < ActiveRecord::Base
  end
end

# config/routes.rb
resources :users

# app/views/users/index.html.haml
= url_for(@user)    # undefined method 'm_users_path'

是否可以让url_for忽略M::User中的模块并为url_for(@user)而不是m_user_path返回user_path?

Is it possible to have url_for ignore the module in M::User and return user_path for url_for(@user) instead of m_user_path?

更新

因此,将近5年之后,感谢esad,这是解决方案.这已经在Rails 4.2中进行了测试.

So, after almost 5 years, here's the solution, thanks to esad. This has been tested in Rails 4.2.

# app/models/m/user.rb
module M
  class User < ActiveRecord::Base
  end
end

# app/models/m.rb
module M
  def self.use_relative_model_naming?
    true
  end
  def self.table_name_prefix
    'm_'
  end
end

# config/routes.rb
resources :users

# app/views/users/index.html.haml
= url_for(@user)    # /users/1

注意:当使用bin/rails g scaffold m/user生成模型,视图和控制器时,视图和控制器也将被命名.您需要将app/views/m/users移至app/views/users并将app/controllers/m/users_controller.rb移至app/controllers/users_controller.rb;您还需要在除模型M::User之外的所有地方删除对模块M的引用.

Note: when generating model, view and controller with bin/rails g scaffold m/user, the views and the controller will be namespaced, too. You need to move app/views/m/users to app/views/users and app/controllers/m/users_controller.rb to app/controllers/users_controller.rb; you also need to remove references to the module M everywhere except in the model M::User.

最后,这里的目标是命名空间模型,而不是视图和控制器.使用esads解决方案时,显式告知模块M(包含User)不要出现在路由中.因此,实际上,M被除去,仅剩下User.

Finally, the goal here was to namespace models but not views and controllers. With esads solution, the module M (containing User) is explicitly told to not appear in routes. Thus, effectifely, the M is stripped of and only User remains.

用户模型现在可以驻留在app/views/models/m/user.rb中,用户控制器驻留在app/views/controllers/users_controller.rb中,而视图可以在app/views/users中找到.

The user model can now reside in app/views/models/m/user.rb, the users controller lives in app/views/controllers/users_controller.rb and the views can be found in app/views/users.

推荐答案

只需在包含模块中定义use_relative_model_naming?,以避免在生成的路由名称前加上前缀:

Just define use_relative_model_naming? in the containing module to avoid prefixing the generated route names:

module M
  def self.use_relative_model_naming?
    true
  end
end

这篇关于Rails url_for和命名空间模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆