protect_from_forgery
“application”
end
What I am missing?
Is it possible that your before_filter … unless test is being run
before set_layout is called? You could display some debug output to
find out.
unless condition seem does not work.
What I am missing?
The unless will run at class-definition time, not on each request. It
also runs in the context of the class, not the instance - so @current_layout will always be nil.
To do conditional activation of callbacks, try the :if or :unless
options. These take either a symbol representing a method or a proc.
end
else
session[:current_layout] = nil
unless condition seem does not work.
What I am missing?
Is it possible that your before_filter … unless test is being run
before set_layout is called? You could display some debug output to
find out.
It seems so, I’ve tried before_filter :set_layout, :authenticate_user!
but it seems that @current_layout is always nil when skip_filter is
run, it is strange to me because set_layout is run before
authenticate_user I think.
because I didn’t understand very well what are you doing.
I’m trying to do:
in route.rb I have: match “/:layout” => “companies#index”
Then when I call localhost/intraOp ther params[:layout] is “intraOp”,
when I call localhost/interOp the params[:layout] is “interOp”.
In application_controller I set:
before_filter :set_layout, :authenticate_user! #(authenticate_user is
from devise gem)
def set_layout
if params[:current_layout] == “intraOp”
session[:current_layout] = :intra
elsif params[:current_layout] == “interOp”
session[:current_layout] = :inter
else
session[:current_layout] = nil
end @current_layout = session[:current_layout]
end
end
Then I have @current_layout set to “intraOp” or “interOp”, if neither
intraOp or interOp is set then @current_layout is nil.
In application_controller I have also before_filter :set_layout,
:authenticate_user!.
In companies_controller I’ve set skip_filter :authenticate_user!,
only[:index, :show] but I want not to skip if @current_layout is nil.
@current_layout is nil but authenticate_user! in companies_controller
is skipped despite :unless => lambda {@current_layout.nil?}
Perhaps skip_filter does not accept conditionals unless or if?
I think you are right, I don’t see it in the docs. Possibly a better
way anyway, which might make more readable code, would be to not call
authenticate directly from the filter but to call a method you provide
which checks the params and calls authenticate if necessary. Then all
the logic is in one place.