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)
1. Do you have the relationship declared in the models?
class Note < ActiveRecord
belongs_to :user
end
class User < ActiveRecord
has_many :notes
end
2. If @user is a nil object, it will throw an error. Set @user to
the current_user in your before_filter. If there is a possibility of
hitting that point with a nil user, then change the line to read
something like this:
I tried your suggestion "@note = @user ?
@user.notes.find(params[:id]) : nil " and while it didn't error which
was good, it didn't work, it ended up redirecting... Full code:
class NotesController < ApplicationController
before_filter :correct_user, :only => :show
.
.
.
def show
@note = Note.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @note }
end
end
You don't seem to be setting @note_userid anywhere. The check is
redundant though - doing @user.notes.find(...) ensures that the
returned note (or notes) have a user_id that is @user.id. You
obviously need to set @user to somethign first (or if current_user is
set appropriately by your authentication stuff then you could do
current_user.notes.find(...).