This is a simple tutorial for RoR beginners.
This tutorial is meant for those using the Salted Hash Login Generator.
Problem: You need a login system that will show the user their information only. For example: A user has many blog entries and they should only be able to edit their blog entry.
Solution: Very simple! You just asign a foreign key to the model associated with the user (for this we'll use user_id).
After you install and configure the Slated Hash Login Generator add the following:
1. Relate the Post model with the User model using has_many and belongs_to.
/app/models/user.rb
Last edited by patrick@iws (2006-11-12 03:48:20)
Offline
Great Post! Do you think this would also work with the Model Security Gem written by Bruce Perens?
Offline
Thanks again I was able to get it working! Do you think you will make any other ones like this?
Offline
I will be posting a complete tutorial on authentication shortly after thanksgiving. It will be a rather long one, so be prepared to spend at least an hour digging through the code and making fit for your use.
And yes, this same method will work with any authentication gem. You just have to make sure you are referring to the same session set in the authentication controller.
Offline
Thanks again!
I can't wait to read your other postings. I hope the posting you are referring to will include how to add more types of before_filters.
Offline
I got a problem with my first try of this.
It works well but...
Ruby version 1.8.5 (i386-mswin32)
Rails version 1.2.2
Database adapter oci
I'm using Oracle and salted_login_generator 1.1.1
As you can see below users have one role:
Me wrote:
@session['user'].role.title
it throws NoMethodError, meanwhile
Me wrote:
@session['user'].role.id
works well.
Where did I mistake?
Ruby wrote:
NoMethodError in Project_man#list
Showing app/views/layouts/project_man.rhtml where line #12 raised:
undefined method `title' for #<User:0x7ac90d4>
Extracted source (around line #12):
9: </head>
10: <body>
11:
12: <p>Hello <%= @session['user'].role.title.to_s + " " + @session['user'].login.to_s %></p>
Offline
Great. I had to deal with the same problem and my personal solution looks exactly the same. Thank you for the confirmation of my work.
Offline