views:

19

answers:

2

Hello, I'm a Rails newbie.... Here's what I'm trying to do....

I created a scaffold for notes (t.text :content, t.integer :user_id)

What I want to do now is only allow user's to view notes that they created. ie (== user_id)

In my /app/controllers/notes_controller.rb

I have the following:

class NotesController < ApplicationController
    before_filter :authenticate
    before_filter :correct_user
.
.
.
.



def correct_user
  @noteuserid = Note.find(:conditions=>["note.user_id=?", @noteuserid])
  redirect_to(root_path) unless current_user?(@noteuserid)
end

I'm having problems understanding how to write the following line: @noteuserid = Note.find(:conditions=>["note.user_id=?", @noteuserid])

Any ideas?

Thanks

+1  A: 

So, firstly you want to find the Note being accessed by the user, then check whether that Note is valid for the user. I would try something like this (assuming that your current_user? method checks whether a given user id matches the current logged in user:

def correct_user
  current_note = Note.find(params[:id])
  redirect_to(root_path) unless current_user?(current_note.user_id)
end

Also, you may want to watch out for filtering all actions in the controller with your correct_user filter as actions to create a note may not have an id of a note to check against. Additionally, when you are viewing a collection of notes you will need to filter differently (e.g. Note.find(:all, :conditions => { :user_id => current_user_id })). It may be more appropriate to apply the correct logic in specific actions rather than as a generic filter.

Finally, you could look at the cancan plugin which would do a lot of the hard work for you with code like this.

Shadwell
Thanks, the plug-in looks cool. right now I want to learn the basics before using any plug-ins which is why I'm going through this exercise. Strangely, the code above isn't working, it's always redirecting back to the root_path, is there an easy way to debug this kind of issue in Rails?
AnApprentice
The first thing I'd do in this case is start with the logs. Check that the queries you would expect are being logged. Rails will log if a filter redirects so it would be worth checking that the filter you expect is redirecting to root. Failing that start adding your own logging to diagnose the problem. E.g. is your current_user? method receiving and returning what you would expect.
Shadwell
+2  A: 

In Rails 3:

Note.where(:user_id=>current_user)

Or, you can start with the user...

User.find(current_user_id).notes.find(note_id)
DGM
Where would this go? Any way you can right out the entire block like above? I'm new to Rails so that would be a huge help to see what you're thinking
AnApprentice
It depends on how you define your users. If you haven't solved that one, you need to. I assume you somehow have a session set up to verify who the user is...
DGM
Right now I'm using the system in the Rails 3 Tutorial Book: http://railstutorial.org/chapters/sign-in-sign-out#sec:current_user
AnApprentice
seems like the problem is current_user is an object, not an ID... Not sure how to make the two comparable...
AnApprentice
Rails seems to reject this: "current_userid = current_user.id"
AnApprentice