1

以下のコードを実行しようとするたびに、上記のエラーが発生し続けます。フォームから情報を削除しようとしています。「destroy」メソッドを見ることができますか?

class ArticlesController < ApplicationController

  def show
    @article = Article.find(params[:id])
  end

  def new   
    @article = Article.new
  end

  def create
    @article = Article.new(params[:article])
    @article.save
    redirect_to article_path(@article)
  end

  def destroy 
    @article = Article.new(params[:article])
    @article.delete
    @article.save
    redirect_to article_path(@article)   
  end

  def edit
    @article = Article.find(params[:id]) 
  end   
end
4

2 に答える 2

2

削除または破棄されたモデルを更新または保存することはできません。ラインを外すだけ@article.save

また、destroy メソッドで、次の行で削除するためだけに Article の新しいインスタンスを作成するのはなぜですか? destroy メソッドにはこれだけが必要です

def destroy
  @article.delete
  redirect_to article_path(@article)
end

コントローラーの代わりにモデルで destroy メソッドを定義して、単に次のように言うこともできます。

def destroy
  self.delete
end
于 2013-08-19T00:27:12.067 に答える
0

凍結されたハッシュの問題を変更できないという問題が発生しました。これは、それを修正するために使用した回避策/ハックです。これは回避策であり、最終的な解決策ではありません。

テーブルを削除します: - Rails コンソールから: ActiveRecord::Migration.drop_table(:table_name)

モデル ファイル番号を 1 増やし、ファイル名を変更します: - db/migrate/1234_create_table_name.rb -> 1235_create_table_name.rb

rake db:移行

于 2015-04-18T06:59:43.267 に答える