0

对于我的收件箱,我创建了一种从收件箱中删除多条消息的方法。现在我正在尝试设置如何删除当前用户正在查看的消息。消息底部应该有一个链接,内容为Delete。当用户单击Delete链接时,消息将被删除,用户将被重定向到他们的收件箱。

将代码添加到控制器时,我现在什至无法查看收件箱消息。我undefined local variable or method "message" error在尝试查看消息时收到一条消息,页面标题为NameError in Messages#show.

有谁知道我该如何解决这个问题?

添加到控制器的新代码:

   def destroy
     @message = Message.find(params[:id])
     @message.destroy
     flash[:notice] = "Successfully deleted message."
     redirect_to user_messages_path(@user, @messages)
   end

消息控制器:

Class MessagesController < ApplicationController
  before_filter :set_user

  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

  private
    def set_user
      @user = current_user
    end
end

显示.html:

<%= link_to "remove", message, :confirm => 'Are you sure?', :method => :delete %>

路线:

Dating::Application.routes.draw do
  get 'signup' => 'users#new'
  get 'login' => 'sessions#new'
  get 'logout' => 'sessions#destroy'
  get 'edit' => 'users#edit'
  get "/profile/:id" => "users#show"
  get "profile/:id/settings" => 'users#edit'
  match 'settings/:id' => 'users#settings'

  resources :users
  resources :sessions
  resources :password_resets
  resources :galleries
  resources :photos
  resources :searches

  resources :users do  
      get 'settings', on: :member  
  end

  root to: 'users#new'
  root to: 'galleries#index'

  resources :users do |user|
    resources :messages do
      collection do
        post 'delete_multiple'
      end
    end
  end
4

1 回答 1

3

我认为你需要@message像这样在你的 link_to 中使用:

<%= link_to "remove", @message, :confirm => 'Are you sure?', :method => :delete %>
于 2013-04-17T14:12:30.090 回答