【问题标题】:Simplifying code in Rails ApplicationController for URL Paths为 URL 路径简化 Rails ApplicationController 中的代码
【发布时间】: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


    【解决方案1】:
    !request.path.start_with?("/admin", "/users")
    

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

    【讨论】:

    • 没问题@ChristopherWarrington,如果适合您的目的,请不要忘记将问题标记为“已回答”。
    • 在将代码标记为已回答之前,我一直在等待测试代码以确保它在我的应用程序中运行。问题是,由于另一个问题,我现在无法测试您的代码。截至目前,我的代码在登录上运行,用户在登录之前不能在/admin/user 页面上。但是,他们可以在注销之前。而且由于我的代码在有人注销时没有运行,所以我无法对其进行测试。 :-) 有道理。有一次,我找出了问题所在,可以测试你的代码,看到它是正确的,我当然会把它标记为正确答案。
    • 哦,这是另一个问题的链接:Redirect works for Login but not Logout。不过,非常感谢@Rots 的回答。非常感谢您的指导。
    • @ChristopherWarrington 我知道在解决其他问题之前,您无法对此进行测试。我已经回答了你的另一个问题。
    • 您对另一个问题的回答也很准确。谢谢你。修复后,您的代码似乎可以正常工作并且可以验证。因此,我已将此问题标记为已回答。我确实对我在此重定向代码中发现的异常的另一个问题发表了评论。在将其作为另一个问题发布之前,我将对其进行处理。学习的唯一方法是首先尝试自己弄清楚,对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多