Ruby on Rails session

4 views
Skip to first unread message

Christine Gerpheide

unread,
Mar 20, 2011, 1:11:12 PM3/20/11
to grinnellplan...@googlegroups.com
We just had our first Ruby on Rails intro, taught my Anna. My albeit brief meeting minutes are on the wiki. Anna has also offered to compile a list of useful ruby resources, soon to come.

Anna Carey

unread,
Mar 21, 2011, 11:38:35 AM3/21/11
to grinnellplan...@googlegroups.com
It looks like Ian beat me to it. I added a few things to the list,
but Ian had most of them covered.
https://github.com/annaswims/GrinnellPlans/wiki/Development-resources


Also, someone asked about sqlite clients, and my answer at the time
was that they aren't necessary. While somewhat true, it probably
wasn't that helpful. You can use "sqlite3" on the command line, or
use whatever database & client you like. For instance, if you'd
rather use mysql, just add the mysql gem to the Gemfile, change the
adapter in /config/database.yml, so the entry would look like:
development:
adapter: mysql
database: rails_development
username: my_username
password: my super secret password
Then, you'll need to run "bundle install" and "rake db:setup". Just
don't commit these changes.

> --
> You received this message because you are subscribed to the Google Groups
> "GrinnellPlans Development" group.
> To post to this group, send email to
> grinnellplan...@googlegroups.com.
> To unsubscribe from this group, send email to
> grinnellplans-deve...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/grinnellplans-development?hl=en.
>

Christine Gerpheide

unread,
Mar 21, 2011, 12:01:14 PM3/21/11
to grinnellplan...@googlegroups.com
Hiya,

Regarding the sqlite clients, I'll just mention that my favorite is a firefox plugin (just search for sqlite in the plugin search).

Christine

2011/3/21 Anna Carey <aca...@gmail.com>

[aggarwal]

unread,
Mar 23, 2011, 12:27:58 AM3/23/11
to GrinnellPlans Development
I am a little stumped by the 'before_filter :require_user' line in the
plans controller. Google suggests this has something to do with the
authlogic gem we have in our bundle. Could someone please explain how
this gem works or point me to some documentation? Also, how does RAILS
end up rendering the /app/view/account_sessions/new.haml? Which files
are executed between the time of initial request for / and the login
page being rendered?

Thanks,
Shitanshu

Anna Carey

unread,
Mar 23, 2011, 11:29:06 AM3/23/11
to grinnellplan...@googlegroups.com, [aggarwal]
'before_filter' means "call the named method(s) before the method you
actually called. In this case it calls require_user in
application_controller.rb.

Some ruby specific things that are worth noting:
Ruby lets you be super lazy, it may be confusing at first, but in the
end you'll think of it as "concise".
*For example If a ruby method doesn't have a "return" specified. If
you don't specify a "return" the value of the last statement evaluated
will be returned.
* "current_account.nil?" is the same as "current_account().nil?"
* "@foo ||= bar " means if @foo is nil, @foo=bar
* Every controller inherits from ApplicationController, so all of
ApplicationController's methods are available to controller classes.
* "new_account_session_path" is a method name automagically created
because of the last line of /cofig/routes.rb . If you want to see all
available routes for a project, you can "rake routes" on the command
line. For more on how routes work, check out the ActionDispatch and
ActionController chapter in the rails book.

So, to put that all together, what "before_filter :require_user" does
is set the values of @current_account_session and @current_account.
It will redirect the user to the log in page if they're not logged in.
If they are logged in , then rails will continue with whatever method
was originally called.

Here's the documentation for Autlogic -
https://github.com/binarylogic/authlogic . In general, it's a safe
bet that the documentation for a gem is on github.com. Also, I'd
highly recommend http://railscasts.com when you're getting started.
There's an autlogic railscast at
http://railscasts.com/episodes/160-authlogic .

I'm guessing you knew quite a bit of this already, but I figured I'd
spell it out for others too.

Ian Young

unread,
Mar 23, 2011, 11:47:05 AM3/23/11
to grinnellplan...@googlegroups.com, [aggarwal]
You've hit on a couple very Rails-y idioms here. Let's go through one by one.

On Tue, Mar 22, 2011 at 9:27 PM, [aggarwal] <saggar...@gmail.com> wrote:
I am a little stumped by the 'before_filter :require_user' line in the
plans controller.

`before_filter` is one of the methods that tells Rails to execute a given callback before or after the controller handles a request[1]. So in this case, before Rails passes off to any of the PlansController actions, it calls the `require_user` method. You'll find this method in the ApplicationController. It simply checks to see if Authlogic knowns about a currently logged-in user, and if not bumps them back to the login screen instead of fulfilling the request.
 
Google suggests this has something to do with the
authlogic gem we have in our bundle. Could someone please explain how
this gem works or point me to some documentation?

Unfortunately, authentication is a bit more complex than typical Rails models. Authlogic takes care of most of the hard stuff for us, so hopefully you won't have to mess with it too much. The most important thing to know is that Account is the model that stores user information, and AccountSession is the special authlogic model that represents a login session, so making a new AccountSession is actually the process of logging in. If you want more documentation, the two best sources are the Readme on github[2] and the Readme on the example project[3].

Also, how does RAILS
end up rendering the /app/view/account_sessions/new.haml?

So because I set up a special routing rule, the "/account_session/login" path is actually a renamed "/account_session/new" - you can see this by running `rake routes`. Rails runs the "new" action in the PlansController, and then unless instructed otherwise it will look for a view file with the action name (in this case, "new").
 
Which files
are executed between the time of initial request for / and the login
page being rendered?

So the request for / gets routed to the PlansController "show" action because of a rule I set in routes.rb. The PlansController, because of the before_filter, will check for a logged-in user. When it doesn't find one, it sends a redirect header to the browser telling it to get "account_session/login" instead. The browser initiates a second request for that resource, which Rails satisfies with the "new" action and view.
 

Thanks,
Shitanshu

If you want further reading on any of the topics I mentioned, I'd recommend the Rails Guides[4]. There are guides on routing, controllers, and rendering, for example.

Ian
 
[2]: https://github.com/binarylogic/authlogic#readme
[3]: https://github.com/binarylogic/authlogic_example#readme
[4]: http://guides.rubyonrails.org/

Reply all
Reply to author
Forward
0 new messages