at.*_*at. 19 ruby-on-rails naming-conventions convention-over-configur ruby-on-rails-3 ruby-on-rails-3.2
Just curious about the best practices for Rails in where I put a custom constraints class that's used as a constraint in config/routes.rb. Seems like Rails.root/lib is where all user classes go. Is that appropriate for this? Should I be creating a directory inside for constraints? 2 empty directories exist there now, assets and tasks. Are there conventions for this?
axs*_*uul 21
lib/将是适当的地方.如果你想让它更干净,把它放进去lib/constraint/authenticated.rb并定义你的约束
module Constraint
class Authenticated
def matches?(request)
# stuff
end
end
end
Run Code Online (Sandbox Code Playgroud)
在你的 routes.rb
constraints Constraint::Authenticated.new do
match 'account' => 'account#index'
end
Run Code Online (Sandbox Code Playgroud)
小智 11
奇怪的是,指南没有说明该路径,但是lib/constraints甚至列在API中(ActionDispatch :: Routing :: Mapper :: Scoping):
如果路由过于复杂,您可以将此逻辑移出到类中.此类必须在
matches?其上定义一个方法,如果应该授予用户访问该路由的权限,则返回true;如果用户不应该访问,则返回false.Run Code Online (Sandbox Code Playgroud)class Iphone def self.matches?(request) request.env["HTTP_USER_AGENT"] =~ /iPhone/ end end此代码的预期位置是lib/constraints.
| 归档时间: |
|
| 查看次数: |
4354 次 |
| 最近记录: |