Questions about Chapter 9: Sign in, Sign out of RoR Tutorial | Learn Rails by Example

Hello

Finished reading chapter 9 and im a little bit confused.

  1. In the tutorial, Modules are used instead of creating a Model and
    working in a class inside a module , like in authenticating passwords.
    Wouldn’t that be possible? Or is it unnecessary since we’re not
    messing with a database in terms of storing data?

  2. what is the difference between session[:remember_token] = user.id
    and cookies[:remember_token] ?
    Session stores the cookie locally until we exit the browser and then
    it gets deleted whereas cookies is permanent stored even after we exit
    the browser?

  3. In section 9.3.3 we are being introduced to the curent user with
    the code in the module SessionsHelper
    self.current_user = user

Why do we use the “self”? and not just current_user since we use it as
a variable object and not as an object attribute ? Or is it a way to
say sessions.current_user = user ?

  1. What’s the purpose of def current_user=(user) and of def
    current_user in listing 9.16 ? Couldnt we avoid all that using a
    single method?

Thank you in advance!

On 5 Feb 2011, at 18:09, Filippos [email protected] wrote:

Hello

Finished reading chapter 9 and im a little bit confused.

Not familiar with that particular tutorial but …

  1. In the tutorial, Modules are used instead of creating a Model and
    working in a class inside a module , like in authenticating passwords.
    Wouldn’t that be possible? Or is it unnecessary since we’re not
    messing with a database in terms of storing data?
    Not sure what you are getting at there.

  2. what is the difference between session[:remember_token] = user.id
    and cookies[:remember_token] ?
    Session stores the cookie locally until we exit the browser and then
    it gets deleted whereas cookies is permanent stored even after we exit
    the browser?

Sessions are stored using cookies (either in the cookie itself or by
having a cookie that points at something in the database or memcache).
The cookie supporting a session is set to expire when the browser quits
whereas ‘normal’ cookies need not be. Also, rails handles serialising
arbitrary ruby objects into the session, I don’t think it will do that
for cookie values.

  1. In section 9.3.3 we are being introduced to the curent user with
    the code in the module SessionsHelper
    self.current_user = user

Why do we use the “self”? and not just current_user since we use it as
a variable object and not as an object attribute ? Or is it a way to
say sessions.current_user = user ?

If you didn’t then it would just be creating a local variable called
current_user, rather than calling the current_user= method

  1. What’s the purpose of def current_user=(user) and of def
    current_user in listing 9.16 ? Couldnt we avoid all that using a
    single method?

One’s the setter method, the other is the getter. Doesn’t seem like much
point is combining the two.

Fred

Its the tutorial http://railstutorial.org/

On Feb 5, 9:51pm, Frederick C. [email protected]

On 5 February 2011 18:09, Filippos [email protected] wrote:

Hello

Finished reading chapter 9 and im a little bit confused.

  1. In the tutorial, Modules are used instead of creating a Model and
    working in a class inside a module , like in authenticating passwords.
    Wouldn’t that be possible? Or is it unnecessary since we’re not
    messing with a database in terms of storing data?

Classes that do not have database storage can be in modules or models,
if that is the question you are asking.

Colin

right, ok , that makes sense.
But in this chapter i see that the author is avoiding using a model.
Is this a common practice in rails or just a personal choice of the
author?
What if we want to list the users who are currently logged in our
website ? That would require a model with access to the database
right? So a module wouldnt be of any use since the data would be
lost?

Session Model with CRUD and REST if we want to include a link for our
users to check out other registered users who are browsing the
website… ?