Search code examples
ruby-on-railsruby-on-rails-4before-filter

before_filter not being called Rails 4


I have this in my application controller:

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

I am following this railscast video: http://railscasts.com/episodes/199-mobile-devices

The problem is: When I first load the page it detects mobile browser and gets the request as MOBILE format. But, if I go to any other page on my website, it renders the HTML format instead of MOBILE. If I reload the page, it detects again that the format is MOBILE.

What is the issue here?


Solution

  • You might want to try using before_action instead of before_filter.

    edit: as paul richter mentioned in the comments... before_filter will still work. But be aware that Rails is discouraging the use of before_filter and suggesting developers to use before_action instead.

    source: Ruby on Rails 4.2 Release Notes