【发布时间】:2015-09-30 11:22:48
【问题描述】:
问题
我有以下代码(见下文),想知道是否有任何方法可以简化代码,以便任何以 /admin/ 或 /users/ 前缀开头的页面都会被识别,我不需要列出商场。
说明
主要是因为随着网站的发展和添加更多管理和用户页面,这个列表可能会变成几十个链接,如果不是更多的话。那么,有没有办法让 ruby 知道像这样跳过带有前缀 /admin/ 或 /users/ 的任何内容?
request.path != "/users/..." &&
request.path != "/admin/..." &&
使用的版本
Ruby:ruby 2.2.1p85(2015-02-26 修订版 49769)[x86_64-darwin14]
Rails:Rails 4.2.0
设计:3.4.1
代码
具体代码:
## app/controllers/application_controller.rb
def store_location
return unless request.get?
if (request.path != "/login" &&
request.path != "/logout" &&
request.path != "/register" &&
request.path != "/users/password/" &&
request.path != "/users/password/new" &&
request.path != "/users/password/edit" &&
request.path != "/users/confirmation" &&
request.path != "/profile/" &&
request.path != "/profile/edit" &&
request.path != "/admin/dashboard" &&
request.path != "/admin/moderate_users" &&
request.path != "/admin/moderate_events" &&
request.path != "/admin/moderate_event_items" &&
request.path != "/admin/moderate_companies" &&
request.path != "/admin/moderate_locations" &&
request.path != "/admin/moderate_stories" &&
!request.xhr?) # don't store ajax calls
session[:previous_url] = request.fullpath
end
end
完整的应用程序控制器:
## app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
after_filter :store_location
before_action :configure_permitted_parameters, if: :devise_controller?
def store_location
# store last url - this is needed for post-login redirect to whatever the user last visited.
return unless request.get?
if (request.path != "/login" &&
request.path != "/logout" &&
request.path != "/register" &&
request.path != "/users/password/" &&
request.path != "/users/password/new" &&
request.path != "/users/password/edit" &&
request.path != "/users/confirmation" &&
request.path != "/profile/" &&
request.path != "/profile/edit" &&
request.path != "/admin/dashboard" &&
request.path != "/admin/moderate_users" &&
request.path != "/admin/moderate_events" &&
request.path != "/admin/moderate_event_items" &&
request.path != "/admin/moderate_companies" &&
request.path != "/admin/moderate_locations" &&
request.path != "/admin/moderate_stories" &&
!request.xhr?) # don't store ajax calls
session[:previous_url] = request.fullpath
end
end
protected
def after_sign_in_path_for(resource)
session[:previous_url] || root_path
end
def after_sign_out_path_for(resource)
session[:previous_url] || root_path
end
end
提前感谢您的帮助。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 simplify