在编辑时按下我的文章更新时不会更新

时间:2017-09-16 21:18:03

标签: ruby-on-rails ruby cloud9-ide

我的问题是,在我编辑文章后,它将恢复为旧的自我

这些是我的照片,代表了我在尝试编辑时采取的步骤:

编辑前:

enter image description here

编辑后:

enter image description here

按下更新按钮后: enter image description here

我的代码:

articles_controller:

class ArticlesController < ApplicationController

  def index
    @articles = Article.all
  end 

  def new
    @article = Article.new 
  end

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

  def update
    @article = Article.find(params[:id])
    if @article.save 
      flash[:notice] = "article was updated"
      redirect_to(@article)
    else
      render 'edit'
    end 
  end 

  def create
    @article = Article.new(article_params)
    if @article.update(article_params)
      flash[:notice] = "Article was submitted succsefully"
      redirect_to (@article)
    else
      render 'new'
    end 
  end

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

  private 

  def article_params 
    params.require(:article).permit(:title, :description)
  end

end 

1 个答案:

答案 0 :(得分:1)

'com.android.support:appcompat-v7:26.+'行动中:

update

您永远不会将 def update @article = Article.find(params[:id]) if @article.save flash[:notice] = "article was updated" redirect_to(@article) else render 'edit' end end 分配给article_params。你想做类似的事情:

@article

使用 def update @article = Article.find(params[:id]) @article.update_attributes article_params if @article.save flash[:notice] = "article was updated" redirect_to(@article) else render 'edit' end end 设置属性而不进行保存,以便您可以进行assign_attributes测试。