- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我刚刚在我的博客中引入了类别,并且我已经对帖子进行了友好的处理。所以我决定也将它整合到类别中。问题是我不断收到错误 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/
在检查对象是否为 nil 时,有人使用 1: if (object == nil) { //... } 有人用 2: if (nil == object) { //... } 1和2有
func InsertApData(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body)
为什么 nil.to_s 返回 "",而 nil.inspect 返回 "nil"(当显然 .inspect 使用 .to_s 方法) 最佳答案 "inspect" method of the Obj
运行时间 https://play.golang.org/p/sl12vfS9vP package main import "fmt" func main() { err := run()
我可以将三元条件运算符用于 if {} else {} 语句,如下所示:a ? x : y,还是问题?回答 1 : 回答 2。 是否可以使用这种格式来检查,而不是 a 是 true 还是 false,
无法弄清楚这里出了什么问题。按照设计设置说明,用谷歌搜索我能想到的一切,仍然没有运气。 undefined method `email' for # Extracted source (around
今天写一些 rspec 时,我遇到了一些意外的行为,将日期(和时间)实例与 nil 进行比较。这是一个使用原始 ruby 的示例(没有 Rails 或其他库): user@MacBook-Work
我将数据类型的非零值分配给非可选属性,然后将其分配给可选属性,最后用所述数据实例化图像。当可选项通过 if-let 子句时,它的 block 执行,抛出错误: Fatal error: Unexpec
swift 5.1 。 考虑以下。 let x: Any? = nil let y: Any = x print("x \(x)") // x nil pri
请耐心听我解释这一点, 我正在创建一个聊天室,用户可以在其中上传照片供其他人查看。当用户点击图标时,他们可以将照片上传到我的 Firebase 数据库(这已成功完成,我已经对此进行了测试)。照片的 U
我的 xCode 5.0 目前遇到一个奇怪的问题:一个对象在控制台中似乎为 nil,但同时它可以通过代码访问。 图 1:对象似乎为零 图2:NSLog(@"%@", imgDownloader) 的输
我有一个实现协议(protocol)的类,以便添加 3 个变量。我专门设置了图像变量,调试器显示该变量存在,但是在我打印它时在代码中显示为 nil,我的 if let 语句也认为该变量为 nil。 @
这个问题在这里已经有了答案: Swift 2 ( executeFetchRequest ) : error handling (5 个答案) 关闭 7 年前。 开始在 SWIFT 中学习编码,每次
两者 (not 'nil) 和 (not nil) 求值为T,那么'nil和nil有什么区别吗?那么 ''nil 呢?如果 ''nil 的计算结果为 'nil,那么 ''nil 是否也应该计算为 ni
(if '(nil nil) 'print-true 'print-false) (if '(nil) 'print-true 'print-false) 在上面的代码
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: obj.nil? vs. obj == nil 我发现了一个问题 - == nil 和 nil 哪个更好?
此代码段抛出异常: x = nil jsoned = x.to_json puts 'x.to_json=' + jsoned.inspect puts 'back=' + JSON.parse(js
基本上我有一个对象想要传递给前端。我在后端记录了它,它不是空的,但是在前端,当我提醒它时,它变成了空。 ... presentation := &presentationStruct { Obje
我创建了一个自定义错误类型来包装错误,以便在 Golang 中更轻松地进行调试。当有错误要打印时它可以工作,但现在它会引起 panic 。 Demo type Error struct { E
写一个符合我当前问题的标题有点难..我有一个 main() 函数,它使用另一个包 (database_sql) 中的函数。该函数初始化一个全局变量 sql.DB*。初始化后,变量不为nil,但是对于其
我是一名优秀的程序员,十分优秀!