创建删除链接时消息的未定义局部变量

时间:2013-04-17 14:07:34

标签: ruby-on-rails

对于我的收件箱,我创建了一种从收件箱中删除多条邮件的方法。现在我正在尝试设置如何删除当前用户正在查看的消息。消息底部应该有一个读取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

show.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

1 个答案:

答案 0 :(得分:3)

我认为您需要在link_to中使用@message,如下所示:

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