为 URL 路径简化 Rails ApplicationController 中的代码

Simplifying code in Rails ApplicationController for URL Paths

问题

我有以下代码(见下文),想知道是否有简化代码的方法,以便识别以 /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

提前感谢您的帮助。

!request.path.start_with?("/admin", "/users")

http://ruby-doc.org/core-2.2.2/String.html#method-i-start_with-3F