gpt4 book ai didi

ruby-on-rails - Friendly_Id 和保留字——如何替换保留字?

转载 作者:数据小太阳 更新时间:2023-10-29 07:17:59 26 4
gpt4 key购买 nike

这是导致错误的示例:

ruby-1.9.2-p290 :004 > Post.new(title: "new").save!
(0.3ms) BEGIN
post Load (0.3ms) SELECT `posts`.* FROM `posts` WHERE (`slug` = 'new' OR `slug` LIKE 'new--%') ORDER BY LENGTH(`slug`) DESC, `slug` DESC LIMIT 1
(0.3ms) SELECT 1 FROM `posts` WHERE `posts`.`lsi_post_id` = BINARY '' LIMIT 1
(0.1ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Friendly is reserved

我想在 Post 模型中添加一些东西,可能会用“-”或类似的东西替换新词,但我不确定从哪里开始。

谢谢!

最佳答案

根据 daemonsySizzlePants 的回答,我想到了这个,它悄悄地将“new”重命名为“new2”,将“edit”重命名为“edit2”,并且保持其他一切和以前一样:

class Page < ActiveRecord::Base

extend FriendlyId
friendly_id :friendly_id_title, use: :slugged
def friendly_id_title
case title.parameterize
when 'new' then 'new2'
when 'edit' then 'edit2'
else title
end
end

end

关于ruby-on-rails - Friendly_Id 和保留字——如何替换保留字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8017878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com