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?
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I've tried in both manners but nothing :-(
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
Sorry for my ignorance, how can I display some debug?
Have a look at the Rails Guide on Debugging, it shows several techniques.
Colin
I've tried
skip_filter :authenticate_user!, :only => [:index, :show] :unless =>
lambda {@current_layout.nil?}
But does not work.
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.
Well put in some debug and prove it one way or the other.
Colin
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?}
Perhaps skip_filter does not accept conditionals unless or if?
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.
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