What's wrong?

route.rb

match “/:layout” => “company@index”

in application_controller I have:

before_filter :authenticate_user!
rescue_from DeviseLdapAuthenticatable::LdapException do |exception|
render :text => exception, :status => 500
end
protect_from_forgery
before_filter :set_layout

layout :specify_layout

def specify_layout
if @current_layout == :intra
“intranet”
elsif @current_layout == :inter
“internet”
else
“application”
end
end

def set_layout
if params[:layout] == “intraOp”
session[:current_layout] = :intra
elsif params[:layout] == “interOp”
session[:current_layout] = :inter
else
session[:current_layout] = nil
end
@current_layout = session[:current_layout]
end
end

in another controller I have:

skip_filter :authenticate_user!, :only => [:index, :show] unless
@current_layout.nil?

unless condition seem does not work.
What I am missing?

On Sat, Feb 11, 2012 at 9:46 PM, Mauro [email protected] wrote:

protect_from_forgery
“application”
end
What I am missing?

Maybe it could be :unless => @current_layout.nil? instead of unless
@current_layout.nil?

skip_filter :authenticate_user!, :only => [:index, :show] unless
@current_layout.nil?

I’m guessing that the unless statement is being parsed as an argument
to skip_filter. Try using parentheses like this:

skip_filter(:authenticate_user!, :only => [:index, :show]) unless
@current_layout.nil?

Mike

On 12 February 2012 02:32, Mike K. [email protected] wrote:

skip_filter :authenticate_user!, :only => [:index, :show] unless
@current_layout.nil?

I’m guessing that the unless statement is being parsed as an argument
to skip_filter. Try using parentheses like this:

skip_filter(:authenticate_user!, :only => [:index, :show]) unless
@current_layout.nil?

I’ve tried in both manners but nothing :frowning:

On 11 February 2012 23:46, Mauro [email protected] wrote:

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.

Colin

On 12 February 2012 12:14, Colin L. [email protected] wrote:

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.

Sorry for my ignorance, how can I display some debug?

On 12 February 2012 11:22, Mauro [email protected] wrote:

render :text => exception, :status => 500
“internet”
else

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.

Sorry for my ignorance, how can I display some debug?

Have a look at the Rails Guide on Debugging, it shows several
techniques.

Colin

skip_filter :authenticate_user!, :only => [:index, :show] unless
@current_layout.nil?

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.

–Matt J.

On 12 February 2012 18:59, Matt J. [email protected] wrote:

To do conditional activation of callbacks, try the :if or :unless
options. These take either a symbol representing a method or a proc.

I’ve tried
skip_filter :authenticate_user!, :only => [:index, :show] :unless =>
lambda {@current_layout.nil?}

But does not work.

On 12 February 2012 12:14, Colin L. [email protected] wrote:

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.

On 12 February 2012 19:34, Mauro [email protected] wrote:

render :text => exception, :status => 500
“internet”
else

authenticate_user I think.
Well put in some debug and prove it one way or the other.

Colin

Well put in some debug and prove it one way or the other.

Then:

application_controller:

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

companies_controller:

skip_filter :authenticate_user!, :only => [:index, :show], :unless =>
lambda {@current_layout.nil?}

def index
debugger
… .

end

rails s --debugger

(rdb:2) instance_variables
[:@_params, :@_response_body, :@_response, :@_action_name,
:@_action_has_layout, :@_env, :@current_layout, :@_request, :@_config,
:@_lookup_context, :@_prefixes, :@_headers, :@_routes, :@_status]

(rdb:2) @current_layout
nil

@current_layout is nil but authenticate_user! in companies_controller
is skipped despite :unless => lambda {@current_layout.nil?}

Try this:

before_filter :authenticate_user!
return true if @current_layout.nil?
rescue_from DeviseLdapAuthenticatable::LdapException do |exception|
render :text => exception, :status => 500
end

And then:

skip_filter :authenticate_user!, :only => [:index, :show]

But, I don’t know if the condition should be:

return true if @current_layout.nil?
or
return true unless @current_layout.nil?
*
*
because I didn’t understand very well what are you doing.

Regards,
Everaldo

On 13 February 2012 03:11, Everaldo G. [email protected]
wrote:

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.

On 12 February 2012 22:51, Mauro [email protected] wrote:

@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.

Colin

@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?