编辑时在我的文章上按更新时不更新
Does not update while pressing update on my articles while editing
我的问题是,在我编辑文章后,它恢复到原来的状态
这些是我的照片,代表我在尝试编辑时采取的步骤:
编辑前:
编辑后:
按下更新按钮后:
我的代码:
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
在您的 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
设置属性而不进行保存,以便您可以进行 if @article.save
测试。
我的问题是,在我编辑文章后,它恢复到原来的状态
这些是我的照片,代表我在尝试编辑时采取的步骤:
编辑前:
编辑后:
按下更新按钮后:
我的代码:
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
在您的 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
设置属性而不进行保存,以便您可以进行 if @article.save
测试。