我正在尝试在消息视图上添加撰写框,以便它可以发送引用原始消息ID的新消息。我的代码给了我一个The action 'update' could not be found for MessagesController
。它不应该更新当前记录,因为它应该提交新的消息ID并引用original_message_id
。
我应该更改表单提交的方式吗?在我的工作回复页面上,页面网址为http://localhost:3000/users/1/messages/new?reply_to=3
,并提交新邮件。使用我当前的代码,我被定向到http://localhost:3000/users/1/messages/new
有人可以看看,让我知道我做错了什么吗?
使用撰写框显示消息:
<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>
消息控制器:
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
消息模型:
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
答案 0 :(得分:0)
您确实错过了Controller中的更新方法。实施它应该可以解决您的问题。