Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

before_filter not inheriting from parent controller correctly?

Sorry if this may be a stupid question but I'm unable get my filters to inherit the way the Rails 3 documentation is saying it should.

Specifically I have an Admin controller that was generated via:

rails generate controller admin

I added only a single action to the admin controller, the before filter & 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

I next then created my nested resources under the admin section with:

rails generate scaffold admin/model

While my admin index is indeed getting the filter, the admin/model index (or any other actions) are not. What is happening under the hood here that I must have excluded?

Thanks in advance.

like image 921
Scott Avatar asked Nov 28 '25 21:11

Scott


2 Answers

Make require_admin_creds a protected method, not private.

like image 156
ramn Avatar answered Dec 02 '25 04:12

ramn


Did you change:

Admin::ModelController < ApplicationController 

to

Admin::ModelController < AdminController

?

This creates the inheritance, not placing the model controller into the admin namespace.

like image 25
Sean Hill Avatar answered Dec 02 '25 04:12

Sean Hill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!