namespace
namespace :admin do
resources :articles
end
# GET | /admin/articles | admin/articles#index | admin_articles_path
scope
- ‘/name’
#
scope 'admin' do
resources :articles
end
# 或者
resources :articles, path: '/admin/articles'
# GET | /admin/articles | articles#index | articles_path
- module
scope module: 'admin' do
resources :articles
end
# GET | /articles | admin/articles#index | articles_path
- shallow_path
scope shallow_path: 'admin' do
resources :articles do
resources :comments, shallow: true
end
end
# GET | /articles/:id/comments | comments#index | article_comments_path(article)
# GET | /admin/comments/:id | comments#show | comment_path(comment)
- as
scope as: 'admin' do
resources :articles
end
# GET | /articles | articles#index | admin_articles_path
- shallow_prefix
# 为辅助方法添加指定前缀, 要配合 shallow 一起用
scope shallow_prefix: 'prefix' do
resources :articles do
resources :comments, shallow: true
end
end
# GET | /articles/:id/comments | comments#index | article_comments_path
# GET | /comments/:id | comments#show | prefix_comment_path(comment)
resources
- shallow
# 浅层嵌套
resources :articles do
resources :comments, only: [:index, :new, :create]
end
resources :comments, only: [:show, :edit, :update, :destroy]
# 也可以写成
resources :articles do
resources :comments, shallow: true
# shallow 只影响到自己和自己模块内的m, 也可用 shallow(&block) 嵌套 不可像shallow_prefix那样指定字符串
resources :others
end
# GET | /articles/:id/comments | comments#index | article_comments_path
# GET | /comments/:id | comments#show | comment_path(comment)
ip 黑名单
class BlacklistConstraint
def initialize
@ips = Blacklist.retrieve_ips
end
def matches?(request)
@ips.include?(request.remote_ip)
end
end
Rails.application.routes.draw do
get '*path', to: 'blacklist#index',
constraints: BlacklistConstraint.new
end
# 或者
# *path 是通配符片段,可以在 params 中获取匹配到的路由 params[:path]
get '*path', to: redirect('/'), constraints: lambda {|request| request.remote_ip == '127.0.0.1'}
draw
可以使用 draw 引入外部路由,把路由模块化