1
问题
我有以下代码(见下文),并想知道是否有无论如何简化代码,以便与一个/admin/
或/users/
前缀开头的页面会承认,我不需要全部列出。简化对Rails的ApplicationController代码URL路径
说明
主要是因为随着网站的发展,并增加了更多的管理和用户页面,这个名单可能成为几十个环节长,如果不是更多。那么,有没有办法让红宝石知道跳过任何前缀/admin/
或/users/
像这样?
request.path != "https://stackoverflow.com/users/..." &&
request.path != "/admin/..." &&
版本使用
红宝石:红宝石2.2.1p85(2015年2月26日修订版49769)[x86_64的-darwin14]
的Rails:Rails的4.2.0
设计:3.4 0.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 != "https://stackoverflow.com/users/password/" &&
request.path != "https://stackoverflow.com/users/password/new" &&
request.path != "https://stackoverflow.com/users/password/edit" &&
request.path != "https://stackoverflow.com/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 != "https://stackoverflow.com/users/password/" &&
request.path != "https://stackoverflow.com/users/password/new" &&
request.path != "https://stackoverflow.com/users/password/edit" &&
request.path != "https://stackoverflow.com/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
预先感谢您的帮助。
谢谢@Rots –
没有问题@ChristopherWarrington,作为“回答说,”如果它适合你的目的,不要忘记,以纪念这个问题。 – Rots
我正在等待测试代码,以确保它在我的应用程序中工作,然后将其标记为已回答。问题是,由于我有另外一个问题,我现在无法测试你的代码。截至目前,我的代码运行在Login上,用户登录前不能在'/ admin'或'/ user'页面登录。但是,他们可以在登出之前。而且由于我的代码在某人登出时没有运行,所以我无法测试它。 :-) 合理。有一次,我发现问题并可以测试你的代码,并且看到它是正确的,我当然会将它标记为正确的答案。 –