2013-06-26 45 views
1

我每次尝试运行下面的代码时都会收到上述错误。我正试图从表单中删除信息。你可以看看“摧毁”的方法吗?无法修改冻结散列

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 
+2

为什么在保存@article后删除它? –

回答

0

模型被删除或销毁后无法更新或保存。只需删除@article.save行。

另外,在你的销毁方法中,为什么要创建一个新的文章实例只在下一行删除它?你破坏方法应该只有这

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

您也可以在模型中,而不是控制器定义的破坏方法,简单地说

def destroy 
    self.delete 
end 
0

我经历了不能修改冻结散的问题,这是我用来解决它的解决方法/破解。这是一种解决方法,不是最终解决方案。

删除该表: - 从铁轨控制台:的ActiveRecord :: Migration.drop_table(:TABLE_NAME)

递增1模型文件号,重命名文件: - DB /迁移/ 1234_create_table_name.rb - > 1235_create_table_name.rb

rake db:migrate