这是我收到的错误消息
NameError in DiscussionsController#destroy
undefined local variable or method `discussion' for #<DiscussionsController:0x598de20>
我创建了一个 DiscussionsController
class DiscussionsController < ApplicationController
def destroy
@discussion = discussion.find(params[:id])
if @discussion.present?
@discussion.destroy
end
redirect_to root_path
end
end
我试图让用户删除他们自己的讨论。这是我用来允许删除的视图:
<% if current_user?(discussion.user) %>
<%= link_to "delete", discussion, method: :delete,
confirm: "You sure?",
title: discussion.content %>
<% end %>
当我尝试时,它会转到http://localhost:3000/disc/7
(7 = discussion_id) 并显示该错误。
我的 routes.db 的一部分
resources :discussions, :path => "disc"
我怎样才能解决这个问题?顺便问一下, DiscussionsController 是必需的吗?我创建它只是为了破坏。
以下评论中错误的后评论表
create_table "postcomments", :force => true do |t|
t.text "content"
t.integer "user_id"
t.integer "micropost_id"
t.timestamp "created_at", :null => false
t.timestamp "updated_at", :null => false
t.text "comment_content"
end
评论后模型
class Postcomment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
validates :comment_content, presence: true
validates :user_id, presence: true
validates :micropost_id, presence: true
default_scope order: 'postcomments.created_at ASC'
end