我的问题是,在我编辑文章后,它将恢复为旧的自我
这些是我的照片,代表了我在尝试编辑时采取的步骤:
编辑前:
编辑后:
我的代码:
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
答案 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
测试。