I am trying to add the compose box on the message view so that it can send a new message that references the original message id. The code I have is giving me a The action 'update' could not be found for MessagesController. It should not be updating the current record, as it's supposed to be submitting a new message id and referencing the original_message_id.
Should I change how the form submits itself? As on my working reply page the page url is http://localhost:3000/users/1/messages/new?reply_to=3 and that submits a new message. With my current code I am being directed to http://localhost:3000/users/1/messages/new
Can someone take a look and let me know what I am doing wrong?
Message show with compose box:
<strong>From:</strong>
<%= #message.sender %>
</p>
<p>
<strong>Received:</strong>
<%= #message.created_at.strftime('%B %-d, %Y %l:%M%P') %>
</p>
<p>
<strong>Subject:</strong>
<%= #message.subject %>
</p>
<p>
<strong>Message</strong><br />
<%=h #message.body %>
</p>
<%= form_for #message, :url => new_user_message_path(#user, :reply_to => #message.sender.id) do |f| %>
<% if #message.recipient_id.nil? %>
<p>
To:<br />
<%= f.hidden_field :recipient_id, :value => params[:user_id] %>
</p>
<% end %>
<p>
<%= f.hidden_field :subject %>
</p>
<p>
Reply:<br />
<%= f.text_area :body %>
</p>
<%= submit_tag "Send"%>
<% end %>
<p>
<% if #message.next %>
<%= link_to 'Next', user_message_path(current_user, #message.next) %>
<% end %>
<% if #message.previous %>
<%= link_to 'Previous', user_message_path(current_user, #message.previous) %>
<% end %>
</p>
<p>
<% if #message.recipient == #user %>
<%= link_to "Reply", new_user_message_path(#user, :reply_to => #message.sender.id) %>
|
<% end %>
<%= link_to "Inbox", user_messages_path(current_user, :mailbox=>:inbox)%>
|
<%= link_to "Delete", [current_user, #message], :confirm => 'Are you sure you want to delete this message?', :method => :delete %>
</p>
Messages controller:
before_filter :set_user
def index
if params[:mailbox] == "sent"
#messages = #user.sent_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
elsif params[:mailbox] == "inbox"
#messages = #user.received_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
#elsif params[:mailbox] == "archived"
# #messages = #user.archived_messages
end
if params[:mailbox] == "unread"
#messages = #user.unread_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
end
if params[:mailbox] == "trash"
#messages = #user.deleted_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
end
end
def new
#message = Message.new
if params[:reply_to]
#reply_to = User.find_by_id(params[:reply_to])
unless #reply_to.nil?
#message.recipient_id = #reply_to.id
end
end
end
def create
#message = Message.new(params[:message])
#message.sender_id = #user.id
if #message.save
flash[:notice] = "Message has been sent"
redirect_to user_messages_path(current_user, :mailbox=>:inbox)
else
render :action => :new
end
end
def show
#message = Message.find(params[:id])
#message.readingmessage if #message.recipient == current_user
end
def destroy
#message = Message.find(params[:id])
#message.destroy
flash[:notice] = "Successfully deleted message."
redirect_to user_messages_path(#user, #messages)
end
def delete_multiple
if params[:delete]
params[:delete].each { |id|
#message = Message.find(id)
#message.mark_message_deleted(#message.id,#user.id) unless #message.nil?
}
flash[:notice] = "Messages deleted"
end
redirect_to user_messages_path(#user, #messages)
end
def reply
#message = Message.find(params[:id]).reply(id)
end
private
def set_user
#user = current_user
end
end
Messages model:
attr_accessible :subject, :body, :sender_id, :recipient_id, :read_at,:sender_deleted,:recipient_deleted
validates_presence_of :subject, :message => "Please enter message title"
has_many :notifications, as: :event
belongs_to :user
scope :unread, -> {where('read_at IS NULL')}
scope :not_deleted_by_recipient, where('messages.recipient_deleted IS NULL OR messages.recipient_deleted = ?', false)
scope :not_deleted_by_sender, where('messages.sender_deleted IS NULL OR messages.sender_deleted = ?', false)
belongs_to :sender,
:class_name => 'User',
:foreign_key => 'sender_id'
belongs_to :recipient,
:class_name => 'User',
:foreign_key => 'recipient_id'
def reply
new_message.reply_from_user_id = self.id #save the user id of original repost, to keep track of where it originally came from
end
def self.by_date
order("created_at DESC")
end
# marks a message as deleted by either the sender or the recipient, which ever the user that was passed is.
# When both sender and recipient marks it deleted, it is destroyed.
def mark_message_deleted(id,user_id)
self.sender_deleted = true if self.sender_id == user_id
self.recipient_deleted = user_id if self.recipient_id == user_id
(self.sender_deleted > 0 && self.recipient_deleted > 0) ? self.destroy : self.save!
(self.sender_deleted != 0 && self.recipient_deleted != 0)
end
# Read message and if it is read by recipient then mark it is read
def readingmessage
self.read_at ||= Time.now
save
end
# Based on if a message has been read by it's recipient returns true or false.
def read?
self.read_at.nil? ? false : true
end
def self.received_by(user)
where(:recipient_id => user.id)
end
def self.not_recipient_deleted
where("recipient_deleted = ?", false)
end
def self.sent_by(user)
Message.where(:sender_id => user.id)
end
def next(same_recipient = true)
collection = Message.where('id <> ? AND created_at > ?', self.id, self.created_at).order('created_at ASC')
collection.where(recipient_id: self.recipient_id) if same_recipient
collection.first
end
def previous(same_recipient = true)
collection = Message.where('id <> ? AND created_at < ?', self.id, self.created_at).order('created_at DESC')
collection.where(recipient_id: self.recipient_id) if same_recipient
collection.first
end
end
private
def send_notification(message)
message.notifications.create(user: message.recipient)
end
You are really missing the update method in your Controller. Implementing it should solve your problem.
Related
I am getting a SyntaxError in UserFriendshipsController#index for:
#user_friendship = current_user.user_friendships.all
I am not sure what I missed. Any assistance will be greatly appreciated! I'm still new to rails. Thanks in advance!
Controller
class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!
def index
#user_friendships = current_user.user_friendships.all
end
def accept
#user_friendship = current_user.user_friendships.find(params [:id])
if #user_friendship.accept!
flash[:success] = "You are now friends with #{#user_friendship.friend.name}"
else
flash[:error] = "That friendship could not be accepted"
redirect_to user_friendships_path
end
def new
if params[:friend_id]
#friend = User.find(params[:friend_id])
#user_friendship = current_user.user_friendships.new(friend: #friend)
else
flash[:error] = "Friend required"
end
rescue ActiveRecord::RecordNotFound
render file: 'public/404', status: :not_found
end
def create
if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
#friend = User.find(params[:user_friendship][:friend_id])
#user_friendship = current_user.user_friendships.new(friend: #friend)
if #user_friendship.save
flash[:success] = "You are now friends!"
else
flash[:error] = "There was a problem."
end
redirect_to user_path(#friend)
else
flash[:error] = "Friend required"
redirect_to root_path
end
end
def edit
end
end
end
Index
<% #user_Friendships.each do |friendship| %>
<% friend = friendship.friend %>
<div id="<%= dom_id(friendship) %>" class="friend row">
<div class="span1">
<center><%= link_to image_tag(user.avatar.url(:thumb)), user %></center>
</div>
<div class="span7">
<strong><%= friend.name %></strong><br />
<%if friendship.pending? %>
<em>Frienship is pending.</em> <%=link_to "Delete request", edit_user_friendship_path(friendship) %>.
<% end %>
<% if friendship.requested? %>
<em>Friendship requested.</em> <%= link_to "Accept Friendship", edit_user_friendship_path(friendship) %>.
<% end %>
<% if friendship.accepted? %>
<em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(friendship) %>.
<% end %>
</div>
</div>
<% end %>
Model
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
attr_accessible :user_id, :friend_id, :user, :friend :state
state_machine :state, initial: :pending do
after_transition on: :accept, do: :send_acceptance_email
state :requested
event :accept do
transition any => :accepted
end
end
def self.request(user1, user2)
transaction do
friendship1 = create!(user: user1, friend: user2, state: 'pending')
friendship2 = create!(user: user2, friend: user1, state: 'requested')
friendship1.send_request_email
end
In your index file, you use #user_Friendships which is not the same variable as the one defined in your controller #user_friendship (extra s and capital F).
I've been following along with this tutorial from lynda.com.
I'm in chapter 15: Nesting pages in subjects
So I've followed through and have even resorted to just copying the exercise files they provide you with. However I keep getting this error:
NoMethodError in PagesController#new
undefined method `id' for nil:NilClass
It hi-lights the second line:
def new
#page = Page.new({:subject_id => #subject.id, :name => "Default"})
#subjects = Subject.order('position ASC')
#page_count = Page.count + 1
end
Here's my controller
class PagesController < ApplicationController
layout "admin"
before_action :confirm_logged_in
before_action :find_subject
def index
# #pages = Page.where(:subject_id => #subject.id).sorted
#pages = #subject.pages.sorted
end
def show
#page = Page.find(params[:id])
end
def new
#page = Page.new({:subject_id => #subject.id, :name => "Default"})
#subjects = Subject.order('position ASC')
#page_count = Page.count + 1
end
def create
#page = Page.new(page_params)
if #page.save
flash[:notice] = "Page created successfully"
redirect_to(:action => 'index', :subject_id => #subject_id)
else
#subjects = Subject.order('position ASC')
#page_count = Page.count + 1
render('new')
end
end
def edit
#page = Page.find(params[:id])
#subjects = Subject.order('position ASC')
#page_count = Page.count
end
def update
# Find an existing object using form parameters
#page = Page.find(params[:id])
# Update the object
if #page.update_attributes(page_params)
# If update succeeds, redirect to the index action
flash[:notice] = "Page updated successfully"
redirect_to(:action => 'show', :id => #page.id, :subject_id => #subject_id)
else
# If the update fails, redisplay the form so user can fix problems
#subjects = Subject.order('position ASC')
#page_count = Page.count
render('edit')
end
end
def delete
#page = Page.find(params[:id])
end
def destroy
page = Page.find(params[:id]).destroy
flash[:notice] = "Page '#{page.name}' destroyed successfully"
redirect_to(:action => 'index', :subject_id => #subject_id)
end
private
def page_params
# Same as using "params[:subject]", except that it:
# - raises as error if :subject is not present
# - allows listed attributes to be mass-assigned
params.require(:page).permit(:subject_id, :name, :permalink, :position, :visible)
end
def find_subject
if params[:subject_id]
#subject = Subject.find(params[:subject_id])
end
end
end
Then here's the view:
<% #page_title = "New Page" %>
<%= link_to("<< Back to list", {:action => 'index', :subject_id => #subject_id}, :class => 'back-link') %>
<div class="pages new">
<h2>Create Page</h2>
<%= form_for(:page, :url => {:action => 'create', :subject_id => #subject_id}) do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<div class="form-buttons">
<%= submit_tag("Create page") %>
</div>
<% end %>
</div>
A question similar to this one has been asked here, but I don't see any solutions there.
Any help is greatly appreciated!
I think the problem is in
Page.new({:subject_id => #subject.id, :name => "Default"})
#subject is not defined and you are asking for its id
You have to define #subject.
Put this as first line in your new action
#subject = Subject.find(params[:id])
Right now #subject is undefined therefore it's nil, calling .id on nil is resulting in the error you're seeing.
When users compose a message, how can I setup so that it references the new message_id created for the conversation_id inside the Messages table?
For example User A sends a new message to User B. Message_id 26 is created and the conversation_id will be 26.
show.html.erb:
<h4>Send Message To User</h4>
<p><%= link_to "Message Me", new_user_message_path(#user), :class => "button" %>
new.html.erb:
<%= f.text_field :conversation_id %>
controller:
def index
#messages = Message.scoped
#message = Message.new
if params[:mailbox] == "sent"
#messages = #user.sent_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
elsif params[:mailbox] == "inbox"
#messages = #user.received_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
#elsif params[:mailbox] == "archived"
# #messages = #user.archived_messages
end
if params[:mailbox] == "unread"
#messages = #user.unread_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
end
if params[:mailbox] == "trash"
#messages = #user.deleted_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
end
end
def new
#message = Message.new
#message.conversation_id = params[:conversation_id]
end
def create
#message = Message.new(params[:message])
#message.sender_id = #user.id
if #message.save
flash[:notice] = "Message has been sent"
redirect_to user_messages_path(current_user, :mailbox=>:inbox)
else
render :action => :new
end
end
def show
#new_message = Message.new
#message = Message.find(params[:id])
#message.readingmessage if #message.recipient == current_user
end
def reply
#reply_message = Message.new
#message = Message.new
#message.conversation_id = params[:conversation_id]
end
def destroy
#message = Message.find(params[:id])
#message.destroy
flash[:notice] = "Successfully deleted message."
redirect_to user_messages_path(#user, #messages)
end
def delete_multiple
if params[:delete]
params[:delete].each { |id|
#message = Message.find(id)
#message.mark_message_deleted(#message.id,#user.id) unless #message.nil?
}
flash[:notice] = "Messages deleted"
end
redirect_to user_messages_path(#user, #messages)
end
def update
#message = Message.new
if params[:reply_to]
#reply_to = User.find_by_id(params[:reply_to])
unless #reply_to.nil?
#message.recipient_id = #reply_to.id
end
end
end
Model:
class Message < ActiveRecord::Base
attr_accessible :subject, :conversation_id, :body, :parent_id, :sender_id, :recipient_id, :read_at,:sender_deleted,:recipient_deleted
validates_presence_of :subject, :message => "Please enter message title"
has_many :notifications, as: :event
belongs_to :conversation, inverse_of: :messages
belongs_to :user
scope :unread, -> {where('read_at IS NULL')}
scope :not_deleted_by_recipient, where('messages.recipient_deleted IS NULL OR messages.recipient_deleted = ?', false)
scope :not_deleted_by_sender, where('messages.sender_deleted IS NULL OR messages.sender_deleted = ?', false)
belongs_to :sender,
:class_name => 'User',
:foreign_key => 'sender_id'
belongs_to :recipient,
:class_name => 'User',
:foreign_key => 'recipient_id'
after_create :set_converstation_id
def set_conversation_id
update_column :conversation_id, id
end
def reply
new_message.reply_from_user_id = self.id #save the user id of original repost, to keep track of where it originally came from
end
def self.by_date
order("created_at DESC")
end
# marks a message as deleted by either the sender or the recipient, which ever the user that was passed is.
# When both sender and recipient marks it deleted, it is destroyed.
def mark_message_deleted(id,user_id)
self.sender_deleted = true if self.sender_id == user_id
self.recipient_deleted = user_id if self.recipient_id == user_id
(self.sender_deleted > 0 && self.recipient_deleted > 0) ? self.destroy : self.save!
(self.sender_deleted != 0 && self.recipient_deleted != 0)
end
# Read message and if it is read by recipient then mark it is read
def readingmessage
self.read_at ||= Time.now
save
end
# Based on if a message has been read by it's recipient returns true or false.
def read?
self.read_at.nil? ? false : true
end
def self.received_by(user)
where(:recipient_id => user.id)
end
def self.not_recipient_deleted
where("recipient_deleted = ?", false)
end
def self.sent_by(user)
Message.where(:sender_id => user.id)
end
def next(same_recipient = true)
collection = Message.where('id <> ? AND created_at > ?', self.id, self.created_at).order('created_at ASC')
collection.where(recipient_id: self.recipient_id) if same_recipient
collection.first
end
def previous(same_recipient = true)
collection = Message.where('id <> ? AND created_at < ?', self.id, self.created_at).order('created_at DESC')
collection.where(recipient_id: self.recipient_id) if same_recipient
collection.first
end
end
private
def send_notification(message)
message.notifications.create(user: message.recipient)
end
If you don't have separate models for conversations and messages, how will you discriminate between the creation of a message and the creation of a conversation?
You should have a Conversation Model that has_many messages and references both users.
Anyways, what you're asking is to have a conversation_id when creating a brand new message (or conversation)?
You can do this in two ways:
1.- Change
#message.conversation_id = params[:conversation_id]
to
#message.conversation_id = #message.id
And in the view use a hidden_field_tag (or the helper that does that) for your conversation_id
2.- Set this id directly on the create method
#message.conversation_id = #message.id
(is that the behavior you wanted?)
Also, why do you have #new_message and #message?
Update
As said in the comments:
I have this
<%= link_to "Reply", reply_user_messages_path(#message.sender, :conversation_id => #message) %>
<%= link_to "Reply", reply_user_messages_path(#message.sender, :conversation_id => #message.conversation_id) %>
I need to find a way to merge the two of them. One method sets a conversation_id and the other copies it.
Easiest way to do that, one line if:
<%= link_to "Reply", reply_user_messages_path(#message.sender, conversation_id: #message.conversation_id ? #message.conversation_id : #message.id) %>
I have created a inbox messaging system and it works perfect. However I don't know how to implement a previous and next feature for the messages (so users can go to the next or previous message while viewing one). These are normal functions for viewing messages on the internet. Previous should mean previous by message creation time. Any help would be appreciated.
messages_controller:
before_filter :set_user
def index
if params[:mailbox] == "sent"
#messages = #user.sent_messages
elsif params[:mailbox] == "inbox"
#messages = #user.received_messages
#elsif params[:mailbox] == "archieved"
# #messages = #user.archived_messages
end
if params[:mailbox] == "unread"
#messages = #user.unread_messages
end
end
def new
#message = Message.new
if params[:reply_to]
#reply_to = User.find_by_user_id(params[:reply_to])
unless #reply_to.nil?
#message.recipient_id = #reply_to.user_id
end
end
end
def create
#message = Message.new(params[:message])
#message.sender_id = #user.id
if #message.save
flash[:notice] = "Message has been sent"
redirect_to user_messages_path(current_user, :mailbox=>:inbox)
else
render :action => :new
end
end
def show
#message = Message.find(params[:id])
#message.readingmessage if #message.recipient == current_user
end
def destroy
#message = Message.find(params[:id])
#message.destroy
flash[:notice] = "Successfully deleted message."
redirect_to user_messages_path(#user, #messages)
end
def delete_multiple
if params[:delete]
params[:delete].each { |id|
#message = Message.find(id)
#message.mark_message_deleted(#message.id,#user.id) unless #message.nil?
}
flash[:notice] = "Messages deleted"
end
redirect_to user_messages_path(#user, #messages)
end
private
def set_user
#user = current_user
end
end
message model:
attr_accessible :subject, :body, :sender_id, :recipient_id, :read_at,:sender_deleted,:recipient_deleted
validates_presence_of :subject, :message => "Please enter message title"
belongs_to :sender,
:class_name => 'User',
:foreign_key => 'sender_id'
belongs_to :recipient,
:class_name => 'User',
:foreign_key => 'recipient_id'
# marks a message as deleted by either the sender or the recipient, which ever the user that was passed is.
# When both sender and recipient marks it deleted, it is destroyed.
def mark_message_deleted(id,user_id)
self.sender_deleted = true if self.sender_id == user_id
self.recipient_deleted = true if self.recipient_id == user_id
(self.sender_deleted && self.recipient_deleted) ? self.destroy : self.save!
end
# Read message and if it is read by recipient then mark it is read
def readingmessage
self.read_at ||= Time.now
save
end
# Based on if a message has been read by it's recipient returns true or false.
def read?
self.read_at.nil? ? false : true
end
def self.received_by(user)
where(:recipient_id => user.id)
end
def self.not_recipient_deleted
where("recipient_deleted = ?", false)
end
def self.sent_by(user)
Message.where(:sender_id => user.id)
end
end
show.html (message view):
<strong>From:</strong>
<%= #message.sender %>
</p>
<p>
<strong>Received:</strong>
<%= #message.created_at.to_s(:long) %>
</p>
<p>
<strong>To:</strong>
<%= #message.recipient %>
</p>
<p>
<strong>Message</strong><br />
<%=h #message.body %>
</p>
<p>
<% if #message.recipient == #user %>
<%= link_to "Reply", new_user_message_path(#user, :reply_to => #message.sender) %>
|
<% end %>
<%= link_to "Inbox", user_messages_path(current_user, :mailbox=>:inbox)%>
|
<%= link_to "Delete", [current_user, #message], :confirm => 'Are you sure you want to delete this message?', :method => :delete %>
</p>
In the Message model:
def previous(same_recipient = true)
collection = Message.where('id <> ? AND updated_at > ?', self.id, self.updated_at).order('updated_at ASC')
collection.where(recipient_id: self.recipient_id) if same_recipient
collection.first
end
def next(same_recipient = true)
collection = Message.where('id <> ? AND updated_at < ?', self.id, self.updated_at).order('updated_at DESC')
collection.where(recipient_id: self.recipient_id) if same_recipient
collection.first
end
This is what we use in our Calendar system, very usefull to go Previous / Next with the Apppointments.
This rely on the updated_at column, which is kind of bad (if I update an old message it will confuse the next/previous). Maybe you want to use the created_at column, or your own. With this you could do in your view:
# [...]
<p>
<strong>Message</strong><br />
<%=h #message.body %>
</p>
<p>
<%= link_to 'Next', user_message_path(current_user, #message.next) %>
<%= link_to 'Previous', user_message_path(current_user, #message.previous) %>
</p>
In your controller for the viewing of the message, get 3 messages from the DB, and display the middle entry as the current message. Set the ID's to the previous and next message as variables.
Then in your view make two buttons: previous and next where their link urls are the path to the IDs you saved.
I am working on custom inbox system. When I go to delete message it flashes a message that message is deleted. However I can still view the message from my inbox and by typing in the URL. How do I setup an action so that the message actually permanently deletes or at least gets place inside of a delete folder (that I have no created yet)?
message model:
class Message < ActiveRecord::Base
attr_accessible :subject, :body, :sender_id, :recepient_id, :read_at,:sender_deleted,:recepient_deleted
validates_presence_of :subject, :message => "Please enter message title"
belongs_to :sender,
:class_name => 'User',
:foreign_key => 'sender_id'
belongs_to :recepient,
:class_name => 'User',
:foreign_key => 'recepient_id'
# marks a message as deleted by either the sender or the recepient, which ever the user that was passed is.
# When both sender and recepient marks it deleted, it is destroyed.
def mark_message_deleted(id,user_id)
self.sender_deleted = true if self.sender_id == user_id and self.id=id
self.recepient_deleted = true if self.recepient_id == user_id and self.id=id
self.sender_deleted && self.recepient_deleted ? self.destroy : save!
end
# Read message and if it is read by recepient then mark it is read
def self.readingmessage(id, reader)
message = find(id, :conditions => ["sender_id = ? OR recepient_id = ?", reader, reader])
if message.read_at.nil? && (message.recepient.user_id==reader)
message.read_at = Time.now
message.save!
end
message
end
# Based on if a message has been read by it's recepient returns true or false.
def read?
self.read_at.nil? ? false : true
end
def self.received_by(user)
where(:recepient_id => user.id)
end
def self.not_recepient_deleted
where("recepient_deleted = ?", false)
end
end
message controller:
class MessagesController < ApplicationController
before_filter :set_user
def index
if params[:mailbox] == "sent"
#messages = #user.sent_messages
elsif params[:mailbox] == "inbox"
#messages = #user.received_messages
#elsif params[:mailbox] == "archieved"
# #messages = #user.archived_messages
end
end
def new
#message = Message.new
if params[:reply_to]
#reply_to = User.find_by_user_id(params[:reply_to])
unless #reply_to.nil?
#message.recepient_id = #reply_to.user_id
end
end
end
def create
#message = Message.new(params[:message])
#message.sender_id = #user_id
if #message.save
flash[:notice] = "Message has been sent"
redirect_to user_messages_path(current_user, :mailbox=>:inbox)
else
render :action => :new
end
end
def show
#message = Message.find(params[:id])
end
def delete_multiple
if params[:delete]
params[:delete].each { |id|
#message = Message.find(id)
#message.mark_message_deleted(#message.id,#user_id) unless #message.nil?
}
flash[:notice] = "Messages deleted"
end
redirect_to user_messages_path(#user, #messages)
end
private
def set_user
#user = current_user
end
end
inbox view:
<h2>Your Inbox</h2>
<% if #messages.size == 0 %>
No messages in your Inbox
<% end %>
<% else %>
<%= form_tag delete_multiple_user_messages_path, :method=>:post do %>
<table class="table table-bordered">
<tr>
<th>Delete?</th>
<th>Sent</th>
<th>Sender</th>
<th>Sent</th>
</tr>
<% for message in #messages %>
<tr>
<td><%= check_box_tag "delete[]", message.id %></td>
<td>
<% if message.read? %>
<%= link_to h(message.subject), user_message_path(#user, message) %>
<% else %>
<%= link_to "#{h(message.subject)} (unread)", user_message_path(#user, message) %>
<% end %>
</td>
<td><%=h message.created_at.to_s(:long) %></td>
</tr>
<% end %>
</table>
<%= submit_tag "Delete selected" %> |
<% end %>
<%= link_to "Compose New Message", new_user_message_path(#user)%> |
<%= link_to "View Sent Messages", user_messages_path(current_user, :mailbox => :sent)%>
Here we go...
One way is to delete the messages permanently from the database, so that they wont be available further even if one can hit the specific url.
or if you want to do soft-delete the messages instead of permanently deleting from the database, in that case, you have to add one condition in the show method so that if a message is soft-deleted, then index page.
so your show method could be
def show
#message = Message.where(:id => params[:id], :deleted => false).first
redirect_to messages_path unless #message.present?
end
in the above query, i assumed that deleted is the field which can be used to tell weather one has deleted or not. And the above query will return a message if only that deleted is false.
UPDATE:
After checking your method mark_message_deleted in model, modify the method as below
def mark_message_deleted(id,user_id)
self.sender_deleted = true if self.sender_id == user_id
self.recepient_deleted = true if self.recepient_id == user_id
(self.sender_deleted && self.recepient_deleted) ? self.destroy : self.save!
end
this one alone is sufficient.