【问题标题】:before_filter not being called Rails 4before_filter 不被称为 Rails 4
【发布时间】:2015-03-05 20:49:47
【问题描述】:

我的应用程序控制器中有这个:

class ApplicationController < ActionController::Base
    helper_method :mobile_device?
    before_filter :prepare_for_mobile

    private
    def mobile_device?
        request.user_agent=~ /Mobile|webOS/
    end

    def prepare_for_mobile
        request.format = :mobile if mobile_device?
    end
end

我正在关注这个 railscast 视频:http://railscasts.com/episodes/199-mobile-devices

问题是: 当我第一次加载页面时,它会检测到移动浏览器并以 MOBILE 格式获取请求。但是,如果我转到我网站上的任何其他页面,它会呈现 HTML 格式而不是 MOBILE。如果我重新加载页面,它会再次检测到格式为 MOBILE。

这里有什么问题?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 before-filter


    【解决方案1】:

    您可能想尝试使用before_action 而不是before_filter

    编辑:正如 cmets 中提到的 paul richter...before_filter 仍然有效。但请注意,Rails 不鼓励使用before_filter,并建议开发人员改用before_action

    来源Ruby on Rails 4.2 Release Notes

    【讨论】:

    • 从技术上讲,before_filter 仍然有效,它还没有从核心中删除(尽管应用程序应该使用 before_action 以防被删除)。
    • 这是真的...对于其他需要帮助的人... rails 4.2 不鼓励使用before_filter,如Rails 4.2 Release Notes 所示
    猜你喜欢
    • 2013-05-07
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 2011-01-24
    相关资源
    最近更新 更多