Underscore character in session var


#1

I stumbled across a weird problem

i can do:
@session[:userid] = 1

but cant:
@session[:user_id] = 1

The second session var doesnt set.
Is there a config setting for this?


#2

are you sure ? i use :user_id


#3

yes…

@session[:user_id] = 1
user_id: <%= @session[:user_id]%>

wont work and

@session[:userid] = 1 (in the controller)
user_id: <%= @session[:userid]%> (in the view)

does work…


#4

Are you sure you don’t unset :user_id, or set :userid in a filter/other
place?


#5

@session might not be a hash, but I think you can do:

debug(@session)

If you need that.

And you can do:

before_filter(:except => [:login,:logout]) {
authorize_by_type(“administrator”)
}

But what are you doing in the authorize_by_type method? Does that method
work?


#6

PS:

before_filter(:except => [:login, :logout]) {
authorize_by_type(:administrator)
}

Is nicer because you are using a role (administrator), and there is only
one role named administrator.

I think a good rule is:

Use symbols if you would use an integer in
#{insert_bad_programming_language}

;-). So you COULD do:

authorize_by_type(1)

And check in that method:

def authorize_by_type(type)
if type == 1

elseif …

end
end

But this is better:

def authorize_by_type(type)
if type == :administrator

elseif …

end
end


#7

Because this works:

before_filter :authorize, :except => [:login,:logout]

thats my other authorize method in the application controller. If so my problem > remains… because when i do this:

before_filter(:except => [:login, :logout]) {
authorize_by_type(:administrator)
}

i get a syntax error, also when i do this:

before_filter(:except => [:login, :logout]) {
:authorize_by_type(‘administrator’)
}

=


#8

Hmmm jup it unsetted somewhere :S
Just blindly copied the code from the agile_web_development book…

i have another question:

in my application.rb file

i have

class ApplicationController < ActionController::Base

def authorize_by_type(name)
…do stuff
end

end

i want to use the authorize_by_type method with the before_filter but my
php syntaxed brain cant find the solution to that. It seems unpossible
to me to get the name var put in.

i tried:

before_filter :authorize_by_type(“administrator”), :except =>
[:login,:logout]
before_filter(:except => [:login,:logout]) {
authorize_by_type(“administrator”) }

it doesnt work

i cant also loop through the session data (after sessiondata has been
set ofcourse)

i tried this in my view

<% for sess in @session %>
<%= sess %>
<% end %>


#9

That doesnt work somehow.

So heres the deal:

application.rb has:

def authorize_by_type(name)

end

in my user_controller i want to use the authorize_by_type method from the
application_controller.

before_filter(:except => [:login, :logout]) {
authorize_by_type(:administrator)
}

now i get this error:

undefined method `authorize_by_type’ for UserController:Class

Doesnt a method from the application_controller need to know the
authorize_by_type method? Or do i have to get the method working with a : before > it?