Hi !! I am working on a project which allows users to keep track of the
courses they are studying.
My Question:
I want to achieve that when user logs into the system he/she can only
view those courses which he/she only added, so that no other users’ data
is messed by someone else.
For e.g. - There are 2 users, Jay and Cathy.
Now, Jay has 5 courses he is studying and Cathy has 2 courses she is
studying. So, when Jay logs into the system with his username/password
he sees only his courses with no access rights to view any other or all
courses in the system.
A wide example of what I want is: del.icio.us … when a user logs in
he/she sees/control only his/her bookmarks. User can’t view/mess someone
else’s bookmarks.
I hope I am clear.
How shall I achieve this ?? Can anyone, please give me an example or
code to look into.
Regards,
Jay.
i plan on doing something similiar. it seems like there is a simple
relationship where you would have a table titled users that is linked to
another with a has_many relationship to a table titled courses. where
after sombody logins, that grants them access to the controller that
selects all courses where id == user id. i think also you may need to
store a session in a file to let your code know that only the user that
is logged in only can view and modify where his name matches up with
whatever is in the row.
do a search for login generator “ruby on rails”. other than that, it may
just be simple sql statements.
Thanks for your reply Koala. I’ll describe what all tables and
relationships I have.
Tables: 1) User 2) Courses
Relationships - Model : User - has_many courses
Model : Course - belongs_to user
DB Table ‘courses’ has user_id.
I am using LoginGenerator gem so when a user logs in - a session[‘user’]
is created which is an array of user.id, user.login and user.password.
Right.
Now, when I have to show only those courses associated to the who logged
in is by putting a SQL statement which will be “SELECT * FROM courses
WHERE user_id = session[‘user’].id” … am i right?
Also, when this logged in user creates a new course then user_id gets
value from session[‘user’].id … is session variable available in model
too, or just controllers or views? How shall I do this?
When I try to achieve all this I get the following stupid error :
Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id
PLEASE HELP:
Jay.
koloa wrote:
i plan on doing something similiar. it seems like there is a simple
relationship where you would have a table titled users that is linked to
another with a has_many relationship to a table titled courses. where
after sombody logins, that grants them access to the controller that
selects all courses where id == user id. i think also you may need to
store a session in a file to let your code know that only the user that
is logged in only can view and modify where his name matches up with
whatever is in the row.
do a search for login generator “ruby on rails”. other than that, it may
just be simple sql statements.
Now, when I have to show only those courses associated to the who logged
in is by putting a SQL statement which will be “SELECT * FROM courses
WHERE user_id = session[‘user’].id” … am i right?
This is precisely the kind of SQL that RoR tries to make unnecessary.
To show the user’s courses (assuming you have a ‘name’ field in each
course) try something like:
in controller: @courses = current_user.courses.find_all
in view:
<% @courses.each do |c| %>
<%= c.name
%>
<% end %>