I have a ChatController and an @user variable in it. On the main page I display @user.name. I also have destroy and create methods that work with ajax, so when I delete a message from my chat, @user becomes nil. To prevent problems from calling name on a nil object, I can add @user=User.find_by_id(:user_id) to every method. But this becomes tedious if I have many methods. Can I declare @user=User.find_by_id(:user_id) once and DRY up my code?
I have a ChatController and an @user variable in it. On the main page
Share
Yes, this is done in a
before_filter(Documentation).Something like:
You may also consider using Inherited Resources which automates this for you.