what's wrong?

18 views
Skip to first unread message

Mauro

unread,
Feb 11, 2012, 6:46:40 PM2/11/12
to rubyonrails-talk
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?

Everaldo Gomes

unread,
Feb 11, 2012, 7:45:50 PM2/11/12
to rubyonra...@googlegroups.com
Maybe it could be :unless => @current_layout.nil? instead of unless @current_layout.nil?
 
--
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.


Mike Kim

unread,
Feb 11, 2012, 8:32:01 PM2/11/12
to Ruby on Rails: Talk
> 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

Mauro

unread,
Feb 12, 2012, 5:54:19 AM2/12/12
to rubyonra...@googlegroups.com

I've tried in both manners but nothing :-(

Colin Law

unread,
Feb 12, 2012, 6:14:57 AM2/12/12
to rubyonra...@googlegroups.com

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

Mauro

unread,
Feb 12, 2012, 6:22:12 AM2/12/12
to rubyonra...@googlegroups.com

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

Colin Law

unread,
Feb 12, 2012, 6:42:04 AM2/12/12
to rubyonra...@googlegroups.com

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

Colin

Matt Jones

unread,
Feb 12, 2012, 12:59:36 PM2/12/12
to Ruby on Rails: Talk

> 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 Jones

Mauro

unread,
Feb 12, 2012, 2:14:12 PM2/12/12
to rubyonra...@googlegroups.com

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

But does not work.

Mauro

unread,
Feb 12, 2012, 2:34:18 PM2/12/12
to rubyonra...@googlegroups.com
On 12 February 2012 12:14, Colin Law <cla...@googlemail.com> wrote:

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.

Colin Law

unread,
Feb 12, 2012, 3:04:58 PM2/12/12
to rubyonra...@googlegroups.com

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

Colin

Mauro

unread,
Feb 12, 2012, 5:33:59 PM2/12/12
to rubyonra...@googlegroups.com
> 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?}

Mauro

unread,
Feb 12, 2012, 5:51:53 PM2/12/12
to rubyonra...@googlegroups.com
> @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?

Everaldo Gomes

unread,
Feb 12, 2012, 9:11:35 PM2/12/12
to rubyonra...@googlegroups.com
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




Mauro

unread,
Feb 13, 2012, 3:13:49 AM2/13/12
to rubyonra...@googlegroups.com
On 13 February 2012 03:11, Everaldo Gomes <everald...@gmail.com> 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.

Colin Law

unread,
Feb 13, 2012, 4:45:19 AM2/13/12
to rubyonra...@googlegroups.com

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

Reply all
Reply to author
Forward
0 new messages