Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use around_action for all controllers except a specific one with some actions

In my application controller i use around_action to set the time zone in every controller action :

class ApplicationController < ActionController::Base
     around_action :user_time_zone

     def user_time_zone(&block)
        Time.use_zone("Central America", &block)
     end
end

but in a specific controller i won't to apply this time zone with some actions, so i want something like this :

around_action :user_time_zone, except: {controller: "foo" & action: "bar" & action: "other_action"}

there is a way to this ?

like image 343
medBouzid Avatar asked Dec 21 '13 04:12

medBouzid


People also ask

What is before action?

A 'before action review' (BAR) is a tool to help a team asses the current knowledge and experience they already have as a way to inform the planning stages of a new project.

What does Before_action do in Rails?

When writing controllers in Ruby on rails, using before_action (used to be called before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It's what you want to use to "prepare" the data necessary before the action executes.

How do I use filters in Ruby on Rails?

Rails after filters are executed after the code in action controller is executed. Just like before filters, after filters are also defined at the top of a controller class that calls them. To set it up, you need to call after_filter method.

What is the role of Rails controller?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.


1 Answers

In specific controller where you do not need this filter just skip it using 'skip_around_filter'

class FooController < ApplicationController
  skip_around_filter :user_time_zone, :only => [:bar]

end

Note: skip_around_filter is supported in Rails 3.2.1 or higher version

like image 70
Allerin Avatar answered Sep 27 '22 22:09

Allerin