0

ここに私が得ているエラーメッセージがあります

NameError in DiscussionsController#destroy

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

私はディスカッションコントローラを作成しました

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 は必要ですか? 私はそれを完全に破壊するために作成しただけです。

以下のコメントのエラーの Postcomments テーブル

 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
4

1 に答える 1

0

discussion大文字の D を使用する必要があります。代わりに:

@discussion = discussion.find(params[:id])

試す:

@discussion = Discussion.find(params[:id])

これはメソッドを呼び出そうとしているクラスであり、Ruby ではクラスは常に大文字で始まります。

于 2013-05-09T01:18:18.557 に答える