Is Before_filter inherited from the parent controller incorrectly?

Sorry if this might be a stupid question, but I can't get my filters to inherit the way the Rails 3 documentation says it should be.

In particular, I have an Admin controller that was generated with:

rails generate controller admin

I added only one action to the administrator controller, before the filter and the private filter method.

class AdminController < ApplicationController

  before_filter require_admin_creds

  def index
  end

private

  def require_admin_creds
    unless current_user && current_user.admin?
        flash[:error] = ...
        redirect_to ....
    end
  end
end

Then I created my nested resources in the admin section using

rails generate scaffold admin/model

As long as my admin index really gets the filter, the admin / model index (or any other actions) are not. What happens under the hood here that I should have ruled out?

Thanks in advance.

+3
source share
3 answers

Make require_admin_creds a protected method, not a private one.

+4

:

Admin::ModelController < ApplicationController 

to

Admin::ModelController < AdminController

?

, admin.

+1

Double check your syntax. You have:

before_filter require_admin_creds

but I think it should be:

before_filter :require_admin_creds

where you use the character, not the name of the variable / method.

0
source

Source: https://habr.com/ru/post/1783053/


All Articles