首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义的局部变量或#<DiscussionsController:0x598de20>的方法“`discussion”

未定义的局部变量或#<DiscussionsController:0x598de20>的方法“`discussion”
EN

Stack Overflow用户
提问于 2013-05-09 09:07:19
回答 1查看 457关注 0票数 0

下面是我收到的错误消息

代码语言:javascript
复制
NameError in DiscussionsController#destroy

undefined local variable or method `discussion' for #<DiscussionsController:0x598de20>

我已经创建了一个DiscussionsController

代码语言:javascript
复制
class DiscussionsController < ApplicationController
  def destroy
    @discussion = discussion.find(params[:id])

    if @discussion.present?
      @discussion.destroy
    end

    redirect_to root_path
  end
end

我正在尝试允许用户删除他们自己的讨论。下面是我用来允许删除的视图:

代码语言:javascript
复制
<% 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的一部分

代码语言:javascript
复制
resources :discussions, :path => "disc"

我该如何解决这个问题呢?顺便问一下,DiscussionsController是必要的吗?我创建它只是为了定义销毁。

以下注释中的错误的Postcomments表

代码语言:javascript
复制
 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

Postcomments模型

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-09 09:18:19

discussion应该是大写的D而不是:

代码语言:javascript
复制
@discussion = discussion.find(params[:id])

尝试:

代码语言:javascript
复制
@discussion = Discussion.find(params[:id])

这是一个您试图调用方法的类,在Ruby中,类总是以大写字母开头。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16452729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档