friendly_id 在更新时创建新条目
friendly_id creates new entry on UPDATE
我正在使用 friendly_id 5.1.0,当我尝试更新一个条目时,例如创建一个新文章,而不是更新条目的数据,它会创建一个新条目。
我已经删除了标题,当我在编辑文章时不更改它时,它会创建一个带有一堆 numbers/letters 的 slug :
http://localhost:3000/articles/first-article-2d392b8e-92b8-44b0-ad67-50dd674f7aaa
这是我的 article.rb 模特:
class Article < ActiveRecord::Base
extend FriendlyId
has_many :comments
friendly_id :title, :use => :slugged
validates :title, presence: true,
length: { minimum: 5}
def should_generate_new_friendly_id?
new_record? || title_changed?
end
当我添加 :use => [:slugged, :history]
时,当我更新条目并保留相同的标题时,它无法保存它,因为我的 :slug
字段是 unique :true
。
这是我的 articles_controller.rb :
class ArticlesController < ApplicationController
def index
@articles = Article.all.order(created_at: :desc)
end
def show
@article = Article.friendly.find(params[:id])
if request.path != article_path(@article)
redirect_to @article, status: :moved_permanently
end
end
def new
@article = Article.new
end
def edit
@article = Article.friendly.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.friendly.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.friendly.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
这是我的 GitHub 存储库和我的(未完成的)项目:https://github.com/TheDoctor314/blog
在参数
中允许id
params.require(:article).permit(:id, :title, :text)
希望对您有所帮助!
编辑表单路由到为文章控制器创建操作而不是更新操作。编辑文件时需要更改表单路径。
如果您看到文章索引操作,您可以看到正在添加新文章,而不是更新
此问题与FriendlyID无关。
您的问题是 here(new
和 edit
都使用的格式):
<%= bootstrap_form_for :article, url: articles_path do |f| %>
它不会尝试使用您的 @article
对象来构建该表单。因此,您的表单总是向 articles_path
发出 POST
请求,每次都会导致 create
。你应该做的是:
<%= bootstrap_form_for @article do |f| %>
这样,表单生成器将检查该对象是否已经 persisted?
,如果是,则生成一个表单,向触发 update
的特定文章发出 PATCH
请求行动。它会尝试自己猜测 URL 。只有当你足够严格地遵循约定时,它才会成功。
如果 @article
不是 persisted?
,它将执行它所做的操作:生成 POST
到 articles_path
。
我正在使用 friendly_id 5.1.0,当我尝试更新一个条目时,例如创建一个新文章,而不是更新条目的数据,它会创建一个新条目。 我已经删除了标题,当我在编辑文章时不更改它时,它会创建一个带有一堆 numbers/letters 的 slug :
http://localhost:3000/articles/first-article-2d392b8e-92b8-44b0-ad67-50dd674f7aaa
这是我的 article.rb 模特:
class Article < ActiveRecord::Base
extend FriendlyId
has_many :comments
friendly_id :title, :use => :slugged
validates :title, presence: true,
length: { minimum: 5}
def should_generate_new_friendly_id?
new_record? || title_changed?
end
当我添加 :use => [:slugged, :history]
时,当我更新条目并保留相同的标题时,它无法保存它,因为我的 :slug
字段是 unique :true
。
这是我的 articles_controller.rb :
class ArticlesController < ApplicationController
def index
@articles = Article.all.order(created_at: :desc)
end
def show
@article = Article.friendly.find(params[:id])
if request.path != article_path(@article)
redirect_to @article, status: :moved_permanently
end
end
def new
@article = Article.new
end
def edit
@article = Article.friendly.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.friendly.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.friendly.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
这是我的 GitHub 存储库和我的(未完成的)项目:https://github.com/TheDoctor314/blog
在参数
中允许id
params.require(:article).permit(:id, :title, :text)
希望对您有所帮助!
编辑表单路由到为文章控制器创建操作而不是更新操作。编辑文件时需要更改表单路径。
如果您看到文章索引操作,您可以看到正在添加新文章,而不是更新
此问题与FriendlyID无关。
您的问题是 here(new
和 edit
都使用的格式):
<%= bootstrap_form_for :article, url: articles_path do |f| %>
它不会尝试使用您的 @article
对象来构建该表单。因此,您的表单总是向 articles_path
发出 POST
请求,每次都会导致 create
。你应该做的是:
<%= bootstrap_form_for @article do |f| %>
这样,表单生成器将检查该对象是否已经 persisted?
,如果是,则生成一个表单,向触发 update
的特定文章发出 PATCH
请求行动。它会尝试自己猜测 URL 。只有当你足够严格地遵循约定时,它才会成功。
如果 @article
不是 persisted?
,它将执行它所做的操作:生成 POST
到 articles_path
。