我对 Rails 破坏操作有疑问。我有完整的消息系统。用户可以发送、阅读消息但不能删除它。我的消息控制器是:
class MessagesController < ApplicationController
before_filter :set_user
def index
if params[:mailbox] == "sent"
@messages = @user.sent_messages.page(params[:page])
elsif params[:mailbox] == "inbox"
@messages = @user.received_messages.page(params[:page])
end
end
def new
@message = Message.new
@reply_to = User.find_by_id(params[:reply_to])
unless @reply_to.nil?
@message.recepient_id = @reply_to.id
end
end
def create
@message = Message.new(message_params)
@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.readingmessage(params[:id],@user.id)
end
def destroy
@message = Message.find(params[:id])
@message.destroy
end
private
def message_params
params.require(:message).permit(:sender_id, :recepient_id, :body, :subject, :read_at)
end
def set_user
@user = current_user
end
end
我的路线是:
resources :users do
resources :messages
end
鉴于我有删除链接(这不起作用):
%h2 Your Inbox
- if @messages.size == 0
%h1 No messages in your Inbox
- else
%table.table.table-bordered
%tr
%th Subject
%th Sender
%th Sent At
- for message in @messages
%tr
%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)
%td= "#{message.sender.name}"
= link_to "Delete Message", @message, method: :delete, data: {confirm: "You sure?"}
当我单击此链接时,出现以下错误:
No route matches [DELETE]
消息的 rake 路由:
user_messages GET /users/:user_id/messages(.:format) messages#index
POST /users/:user_id/messages(.:format) messages#create
new_user_message GET /users/:user_id/messages/new(.:format) messages#new
edit_user_message GET /users/:user_id/messages/:id/edit(.:format) messages#edit
user_message GET /users/:user_id/messages/:id(.:format) messages#show
PATCH /users/:user_id/messages/:id(.:format) messages#update
PUT /users/:user_id/messages/:id(.:format) messages#update
DELETE /users/:user_id/messages/:id(.:format) messages#destroy
有任何想法吗?:)