# Underscore character in session var

**URL:** <https://www.ruby-forum.com/t/underscore-character-in-session-var/52513>\
**Category:** Rails\
**Created:** [January 15, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513 "2006-01-15T17:49:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![timz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@timz](https://www.ruby-forum.com/u/timz)\
**Post date:** [January 15, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513/1 "2006-01-15T17:49:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![timz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@timz](https://www.ruby-forum.com/u/timz)\
**Post date:** [January 15, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513/2 "2006-01-15T18:52:35Z")

</div>

are you sure ? i use :user\_id

---

<div class="post-metadata">

**Author:** ![timz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@timz](https://www.ruby-forum.com/u/timz)\
**Post date:** [January 15, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513/3 "2006-01-15T18:58:55Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![timz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@timz](https://www.ruby-forum.com/u/timz)\
**Post date:** [January 15, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513/4 "2006-01-15T19:00:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![timz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@timz](https://www.ruby-forum.com/u/timz)\
**Post date:** [January 15, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513/5 "2006-01-15T21:43:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![timz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@timz](https://www.ruby-forum.com/u/timz)\
**Post date:** [January 15, 2006, 9:47pm UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513/6 "2006-01-15T21:47:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![timz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@timz](https://www.ruby-forum.com/u/timz)\
**Post date:** [January 16, 2006, 12:04am UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513/7 "2006-01-16T00:04:38Z")

</div>

> 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’)  
}

=

---

<div class="post-metadata">

**Author:** ![timz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@timz](https://www.ruby-forum.com/u/timz)\
**Post date:** [January 15, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513/8 "2006-01-15T19:16:41Z")

</div>

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 %\>

---

<div class="post-metadata">

**Author:** ![timz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@timz](https://www.ruby-forum.com/u/timz)\
**Post date:** [January 16, 2006, 12:00am UTC](https://www.ruby-forum.com/t/underscore-character-in-session-var/52513/9 "2006-01-16T00:00:25Z")

</div>

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?
