未定义的方法`request_uri' ActionDispatch

时间:2016-11-17 16:37:33

标签: ruby-on-rails

我想创建一个我的网站的移动版

我想当有人从移动设备访问我的网站时,它会将他重定向到子域名m.mywebsite.com

但可以选择转到存储在Cookie中的完整网站

这是我的配置

应用程序/控制器/ 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

  before_filter :set_mobile_preferences
  before_filter :redirect_to_mobile_if_applicable
  before_filter :prepend_view_path_if_mobile

  private

    def not_authenticated
      flash[:warning] = 'You have to authenticate to access this page.'
      redirect_to login_path
    end

    def expirated_reset_token
      redirect_to root_path
    end

  # mobile subdomain 
    def set_mobile_preferences
        if params[:mobile_site]
          cookies.delete(:prefer_full_site)
        elsif params[:full_site]
          cookies.permanent[:prefer_full_site] = 1
          redirect_to_full_site if mobile_request?
        end
    end

    def prepend_view_path_if_mobile
        if mobile_request?
          prepend_view_path Rails.root + 'app' + 'mobile_views'
        end
    end

    def redirect_to_full_site
        redirect_to request.protocol + request.host_with_port.gsub(/^m\./, '') +
                    request.request_uri and return
    end

    def redirect_to_mobile_if_applicable
        unless mobile_request? || cookies[:prefer_full_site] || !mobile_browser?
          redirect_to request.protocol + "m." + request.host_with_port.gsub(/^www\./, '') +
                      request.request_uri and return
        end
    end

    def mobile_request?
        request.subdomains.first == 'm'
    end

    helper_method :mobile_request?

    def mobile_browser?
        request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(iPhone|iPod|iPad|Android)/]
      end
    helper_method :mobile_browser?

end

问题出在我访问时

http://m.mywebsite.com/?full_site=1

它给了我

undefined method `request_uri' for #<ActionDispatch::Request:0x007fdb60fa8a00> Did you mean? request_parameters

请帮助

1 个答案:

答案 0 :(得分:0)

ActionDispatch请求实际上并未实现或响应方法request_uri

但它有许多其他方法。其中一个肯定会返回你想要的东西(无论是什么)。以下是供您细读文档的链接:ActionDispatch::Request