Forum: Ruby on Rails Underscore character in session var

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
357e326199208e143c8e967fe3b27b06?d=identicon&s=25 Tim Vo (timz)
on 2006-01-15 17:49
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?
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 Dylan Stamat (Guest)
on 2006-01-15 18:52
(Received via mailing list)
are you sure ?  i use :user_id
357e326199208e143c8e967fe3b27b06?d=identicon&s=25 Tim Vo (timz)
on 2006-01-15 18:58
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...
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules (Guest)
on 2006-01-15 19:00
Are you sure you don't unset :user_id, or set :userid in a filter/other
place?
357e326199208e143c8e967fe3b27b06?d=identicon&s=25 Tim Vo (timz)
on 2006-01-15 19:16
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 %>
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules (Guest)
on 2006-01-15 21:43
@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?
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules (Guest)
on 2006-01-15 21:47
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
357e326199208e143c8e967fe3b27b06?d=identicon&s=25 Tim Vo (timz)
on 2006-01-16 00:00
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?
357e326199208e143c8e967fe3b27b06?d=identicon&s=25 Tim Vo (timz)
on 2006-01-16 00:04
> 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')
}

=
This topic is locked and can not be replied to.