A Good Tutorial on Params and Variables

I have found various posts about instance variables, session variables
and parameters (each of which seems to use differnet syntax and have
different view on the “best practice”) I would like to find a single,
reliable source that provides a good overview on all of it.

Specifically, I would like to learn how to pass parameters between
controllers and how to set session variables so they can be seen from
any Controller/Model.

Thanks

I second that.

Ted Battreall wrote:

I have found various posts about instance variables, session variables
and parameters (each of which seems to use differnet syntax and have
different view on the “best practice”) I would like to find a single,
reliable source that provides a good overview on all of it.

Specifically, I would like to learn how to pass parameters between
controllers and how to set session variables so they can be seen from
any Controller/Model.

Thanks

I don’t have a link for you, but I think it’s simpler than you think…

Post/Get variables are passed in the ‘content’ body of the http request
for POST (form submit) and GET variables are passed in the url. In both
cases, the variables that are passed are immediately available to you
within params[:name_of_passed_param].

If you want to use sessions to store some information you can just
create a new variable in ‘session[:name_of_var]’. So, for example, if
you want to create a new class that will store a bunch of information
you can just do:

session[:user_info] ||= User.new

That will return the User object from the cart if it exists, and if
doesn’t it will create a new one. (Note, if you are embedding a ‘class’
into sessions, you also need to specify a ‘model’ so other controllers
know how to process the chunk of data specified in the session).

Ted Battreall wrote:

I have found various posts about instance variables, session variables
and parameters (each of which seems to use differnet syntax and have
different view on the “best practice”) I would like to find a single,
reliable source that provides a good overview on all of it.

Specifically, I would like to learn how to pass parameters between
controllers and how to set session variables so they can be seen from
any Controller/Model.

Thanks

I am still a little fuzzy on the syntax as I see different syntax all
ofver the place. What is the difference between?

session[‘user’]
and
@session[‘user’]
and
session[:user]

Thanks
Ted

This works fine within the Controller it was assigned it, but it has a
Nil value when I try to reference it in other Controllers. You mentioned
that I need to specify a model. What is the syntax for doing this?

Specifically here is the code:

class UserController < ApplicationController
model :user
layout ‘scaffold’

You’re alreade doing this in the code above. “model :user” is the line I
was referring to. Note that for this class, where you set/create the
user object you don’t have to explicitly specify the model :user, but in
any/all other controllers, this same chunk of bytecode doesnt mean
anything, hence you have to tell it to use ‘model :user’ so it knows
what objects to initialize and how to process that chunk.

So, you can either specify model :user in every other controller that
uses it, or alternatively, if you need it site-wide, put it in the
application.rb which is used to establish site-wide behaviors.

just be careful assigning a model object to a session. Especially if you
have associations and things the sessions can get enormous (has_many,
etc).
Also changes to the model wont be reflected in the session so just be
careful of that as well.

I needed all the attributes from the User model as well in my session so
i
just did something like this to prevent all the associations from making
my
session huge

session[:user] = Hash.new
session[:user] = @user.attributes

Now you can access things like session[:user][“user_id”] and
session[:user][“username”]

adam

One more question. You said “Note, if you are embedding a ‘class’
into sessions, you also need to specify a ‘model’ so other controllers
know how to process the chunk of data specified in the session”.

I think this may be the root of my session problems. I wanted to store
the user information after login, so I can use it throughout my
application.

I used the syntax you described:
session[:user] ||= @user

This works fine within the Controller it was assigned it, but it has a
Nil value when I try to reference it in other Controllers. You mentioned
that I need to specify a model. What is the syntax for doing this?

Specifically here is the code:

class UserController < ApplicationController
model :user
layout ‘scaffold’

def login
return if generate_blank
@user = User.new(@params[‘user’])
if session[‘user’] = User.authenticate(@params[‘user’][‘login’],
@params[‘user’][‘password’])
flash[‘notice’] = l(:user_login_succeeded)
session[:user] ||= @user

class UserNotify < ActionMailer::Base
def invite(person)
@recipients = person.email
@from = @session[:user].email

Session variables are available in all Controllers. Models don’t have
access to session variables though. That would tightly couple your
models to your controllers, and that violates some of the ideas of
seperation behind MVC. If a method in a model needs a session
variable, the controller should pass along that variable when calling
the method.

Session variables are set and read through the session method in the
controller. For example:

session[:user_id] = @user.id

Parameters are very straightforward. Just pass them along in you
redirect_to och link_to (etc.) calls and the receiving controller will
get them:

redirect_to :action => ‘some_action’, :parameter1 => ‘a value’,
:parameter2 => ‘another value’

You get parameters from the params method in the controller:

params[:parameter1]

Hope that makes it a bit clearer. Also, check out
http://weblog.rubyonrails.org/articles/2006/04/25/use-params-not-params

/ CJ

On 4/29/06, Brian H. [email protected] wrote:

Symbols can’t start with digits.

irb(main):004:0> ‘1x’.to_sym
=> :“1x”
irb(main):005:0> :‘1x’
=> :“1x”

On Apr 30, 2006, at 12:59 AM, Ted Battreall wrote:

I am still a little fuzzy on the syntax as I see different syntax all
ofver the place. What is the difference between?

session[‘user’]
and
@session[‘user’]
and
session[:user]

No need for confusion here. All three of these are currently the
same, although the middle form, using @session, has been deprecated
in favor of calling the session method.

Most Rubyists will use the third form, as using symbols in this way
is more efficient than strings, and it just looks better when you
read the source code. In fact, the only time I would use the first
form is when the key I want to reference starts with a digit, instead
of a letter. Symbols can’t start with digits.

-Brian

On Apr 30, 2006, at 04:27 PM, Jeremy E. wrote:

On 4/29/06, Brian H. [email protected] wrote:

Symbols can’t start with digits.

irb(main):004:0> ‘1x’.to_sym
=> :“1x”
irb(main):005:0> :‘1x’
=> :“1x”

I’ve never seen that syntax for symbols before, so it never occurred
to me that you could quote the symbol name. :slight_smile:

I’m not sure I like the look of that, so I don’t think I’ll be using
that form, but it’s good to know that it’s there, in case I need it…

-Brian