i created a messaging model where a user can send a private message to another user. however im not sure how to go about notifying the user he/she got a new message. does anyone have a way to go about doing this? or if there was a simple solution?
def create
@message = current_user.messages.build
@message.to_id = params[:message][:to_id]
@message.user_id = current_user.id
@message.content = params[:message][:content]
if @message.save
flash[:success ] = "Private Message Sent"
end
redirect_to user_path(params[:message][:to_id])
end
i can tell the sender that a private message was sent, but im not sure how i can notify the receiver a new private message was created.
help would be appreciated. thanks = )
First, you can improve your controller like this:
Then, in your models:
This
Notificationmodel allows you to store a user’s notifications for different “events”. You can even store whether a notification has been read or not, or set anafter_createcallback in order to send an email to the notified user.The migration for this
Notificationmodel would be:You can read about the Rails associations options here.