gpt4 book ai didi

ruby-on-rails - friendly_id slugs 不断返回 nil 导致 ActiveRecord 错误

转载 作者:数据小太阳 更新时间:2023-10-29 08:30:36 25 4
gpt4 key购买 nike

我刚刚在我的博客中引入了类别,并且我已经对帖子进行了友好的处理。所以我决定也将它整合到类别中。问题是我不断收到错误 Couldn't find Category with 'id'=test-2 以及 ActiveRecord::RecordNotFound in CategoriesController#show 请理解我有尝试实现我在这里找到的许多解决方案,但我无法让它工作

class CategoriesController < ApplicationController

before_action :find_category, only: [:show, :update, :edit, :destroy]

def index
@categories = Category.all
end

def new
@category = Category.new
end

def create
@category = Category.new(category_params)
if @category.save
flash[:success] = "Category created"
redirect_to categories_path

else
render 'new'
end
end

def edit

end

def update
if @category.update(category_params)
flash[:success]="name was successfully updated"
redirect_to category_path(@category)
else
render 'edit'
end
end

def show

end

private
def category_params
params.require(:category).permit(:name)
end

def find_category
@category = Category.friendly.find(params[:id])
end

end

现在奇怪的是,当我进入 Rails 控制台并尝试更新 slug 时,它显示如下:

Category.find_each(&:save)
Category Load (0.3ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" ASC LIMIT 1000
(0.1ms) begin transaction
Category Exists (0.1ms) SELECT 1 AS one FROM "categories" WHERE ("categories"."id" != 1) AND "categories"."slug" = ? LIMIT 1 [["slug", "sports"]]

上面我们可以看到它说 slug, sports.. 这很好,但是当我找到时... slug 似乎是 nil,如下所示:

2.3.0 :058 > c = Category.find(1)
Category Load (0.8ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", 1]]
=> #<Category id: 1, name: "sports", created_at: "2016-07-28 15:12:13", updated_at: "2016-07-28 15:12:13", slug: nil>

我还试图通过尝试找到真正的 slug 来证实这一点:

2.3.0 :051 > Category.find(1).slug
Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", 1]]
=> nil

slug 似乎为零。

最后,以防万一它是相关的...这是我的 show.html.erb

<h1 align="center"><%= "Category: " + @category.name %></h1>
<div align = "center">
<% if user_signed_in? %>
<div id="admin_links">
<%= link_to "Edit", edit_category_path(@category) %>
<%= link_to "Delete", category_path(@category), method: :delete, data: {confirm: "Are you sure you want to delete?"} %>
</div>
<% end %>

<%= link_to "Edit Category name", edit_category_path(@category) %>

</div>

这是category.rb

class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
has_many :category_posts
has_many :posts, through: :category_posts


def should_generate_new_friendly_id?
slug.blank? || name_changed?
end
end

当我在控制台中执行 Category.first(5) 时,这就是结果

Category Load (0.3ms)  SELECT  "categories".* FROM "categories"  ORDER BY "categories"."id" ASC LIMIT 5
=> [#<Category id: 1, name: "sports", created_at: "2016-07-28 15:12:13", updated_at: "2016-07-28 15:12:13", slug: nil>,
#<Category id: 2, name: "Nerd", created_at: "2016-07-28 17:33:35", updated_at: "2016-07-28 17:33:35", slug: nil>,
#<Category id: 3, name: "testing", created_at: "2016-07-28 17:38:22", updated_at: "2016-07-28 17:39:23", slug: nil>,
#<Category id: 4, name: "test 2", created_at: "2016-07-28 19:10:18", updated_at: "2016-07-28 19:10:18", slug: nil>,
#<Category id: 5, name: "books", created_at: "2016-07-28 19:29:18", updated_at: "2016-07-28 19:29:18", slug: nil>

最佳答案

在您的 Category 模型中,您有 attr_accessor :slug,它将覆盖数据库中用于该字段的 setter 方法。这意味着 slug 字段将不会被保存。

从您的模型中删除 attr_accessor :slug + 再次重新创建实例以重新生成 slug。

关于ruby-on-rails - friendly_id slugs 不断返回 nil 导致 ActiveRecord 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38646457/

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