Friendly_id 5.3 版不工作 rails 6
Friendly_id not working version 5.3 rails 6
我一直在开发一个应用程序,我安装了 friendly_id 5.3 版,一切正常。
但是,最近在开发该应用程序时,我发现 friendly_id 根本无法处理已设置的任何内容。因此,为此,我将使用文章。
我可以使用 http://localhost:3000/articles/1
而不是 http://localhost:3000/articles/another-test
转到文章
这是错误
这是代码,如前所述一切正常,所以我不确定为什么突然间一切都坏了。
article.rb
class Article < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
has_one_attached :article_image
has_rich_text :content
has_many :article_users
has_many :authors, through: :article_users, source: :user
validates :title, presence: true
validates :authors, presence: true
validates :content, presence: true
validates :article_image, presence: true
def name_of_authors
authors.map(&:name).to_sentence
end
def about_authors
authors.map(&:about).to_sentence
end
end
ApplicationController(如果相关)
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :authenticate_user!
protect_from_forgery with: :exception
rescue_from CanCan::AccessDenied do |exception|
redirect_to main_app.root_url, alert: exception.message
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name, :about, :avatar])
end
end
文章控制器
class ArticlesController < ApplicationController
load_and_authorize_resource
before_action :set_article, only: [:show, :edit, :update, :destroy]
skip_before_action :authenticate_user!, only: [:index, :show]
.....
private
# Use callbacks to share common setup or constraints between actions.
def set_article
@article = Article.friendly.find(params[:id])
end
# Only allow a list of trusted parameters through.
def article_params
params.require(:article).permit(:title, :content, :article_image, :author_ids)
end
end
架构
create_table "articles", force: :cascade do |t|
t.string "title"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "slug"
t.index ["slug"], name: "index_articles_on_slug", unique: true
end
create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 50
t.string "scope"
t.datetime "created_at"
t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
t.index ["sluggable_type", "sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_type_and_sluggable_id"
end
任何帮助都会很棒,因为我在这里真的很困惑。
是的,我在控制台中有运行Article.find_each(&:save)
。
irb(main):001:0> Article.find_each(&:save)
Article Load (1.8ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT [["LIMIT", 1000]]
(0.3ms) BEGIN
User Load (3.6ms) SELECT "users".* FROM "users" INNER JOIN "article_users" ON "users"."id" = "article_users"."user_id" WHERE "article_users"."article_id" = [["article_id", 1]]
ActionText::RichText Load (1.8ms) SELECT "action_text_rich_texts".* FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."record_id" = AND "action_text_rich_texts"."record_type" = AND "action_text_rich_texts"."name" = LIMIT [["record_id", 1], ["record_type", "Article"], ["name", "content"], ["LIMIT", 1]]
ActiveStorage::Attachment Load (2.5ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = AND "active_storage_attachments"."record_type" = AND "active_storage_attachments"."name" = LIMIT [["record_id", 1], ["record_type", "Article"], ["name", "article_image"], ["LIMIT", 1]]
(0.2ms) COMMIT
=> nil
cancancan
gem 之间存在冲突。您需要在 config/initializers
中添加一个名为 cancan.rb
的文件,其中包含以下
require_dependency 'cancan/model_adapters/active_record_4_adapter'
if defined?(CanCan)
class Object
def metaclass
class << self; self; end
end
end
module CanCan
module ModelAdapters
class ActiveRecord4Adapter < ActiveRecordAdapter
@@friendly_support = {}
def self.find(model_class, id)
klass =
model_class.metaclass.ancestors.include?(ActiveRecord::Associations::CollectionProxy) ?
model_class.klass : model_class
@@friendly_support[klass]||=klass.metaclass.ancestors.include?(FriendlyId)
@@friendly_support[klass] == true ? model_class.friendly.find(id) : model_class.find(id)
end
end
end
end
end
我一直在开发一个应用程序,我安装了 friendly_id 5.3 版,一切正常。
但是,最近在开发该应用程序时,我发现 friendly_id 根本无法处理已设置的任何内容。因此,为此,我将使用文章。
我可以使用 http://localhost:3000/articles/1
而不是 http://localhost:3000/articles/another-test
这是错误
这是代码,如前所述一切正常,所以我不确定为什么突然间一切都坏了。
article.rb
class Article < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
has_one_attached :article_image
has_rich_text :content
has_many :article_users
has_many :authors, through: :article_users, source: :user
validates :title, presence: true
validates :authors, presence: true
validates :content, presence: true
validates :article_image, presence: true
def name_of_authors
authors.map(&:name).to_sentence
end
def about_authors
authors.map(&:about).to_sentence
end
end
ApplicationController(如果相关)
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :authenticate_user!
protect_from_forgery with: :exception
rescue_from CanCan::AccessDenied do |exception|
redirect_to main_app.root_url, alert: exception.message
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name, :about, :avatar])
end
end
文章控制器
class ArticlesController < ApplicationController
load_and_authorize_resource
before_action :set_article, only: [:show, :edit, :update, :destroy]
skip_before_action :authenticate_user!, only: [:index, :show]
.....
private
# Use callbacks to share common setup or constraints between actions.
def set_article
@article = Article.friendly.find(params[:id])
end
# Only allow a list of trusted parameters through.
def article_params
params.require(:article).permit(:title, :content, :article_image, :author_ids)
end
end
架构
create_table "articles", force: :cascade do |t|
t.string "title"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "slug"
t.index ["slug"], name: "index_articles_on_slug", unique: true
end
create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 50
t.string "scope"
t.datetime "created_at"
t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
t.index ["sluggable_type", "sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_type_and_sluggable_id"
end
任何帮助都会很棒,因为我在这里真的很困惑。
是的,我在控制台中有运行Article.find_each(&:save)
。
irb(main):001:0> Article.find_each(&:save)
Article Load (1.8ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT [["LIMIT", 1000]]
(0.3ms) BEGIN
User Load (3.6ms) SELECT "users".* FROM "users" INNER JOIN "article_users" ON "users"."id" = "article_users"."user_id" WHERE "article_users"."article_id" = [["article_id", 1]]
ActionText::RichText Load (1.8ms) SELECT "action_text_rich_texts".* FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."record_id" = AND "action_text_rich_texts"."record_type" = AND "action_text_rich_texts"."name" = LIMIT [["record_id", 1], ["record_type", "Article"], ["name", "content"], ["LIMIT", 1]]
ActiveStorage::Attachment Load (2.5ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = AND "active_storage_attachments"."record_type" = AND "active_storage_attachments"."name" = LIMIT [["record_id", 1], ["record_type", "Article"], ["name", "article_image"], ["LIMIT", 1]]
(0.2ms) COMMIT
=> nil
cancancan
gem 之间存在冲突。您需要在 config/initializers
中添加一个名为 cancan.rb
的文件,其中包含以下
require_dependency 'cancan/model_adapters/active_record_4_adapter'
if defined?(CanCan)
class Object
def metaclass
class << self; self; end
end
end
module CanCan
module ModelAdapters
class ActiveRecord4Adapter < ActiveRecordAdapter
@@friendly_support = {}
def self.find(model_class, id)
klass =
model_class.metaclass.ancestors.include?(ActiveRecord::Associations::CollectionProxy) ?
model_class.klass : model_class
@@friendly_support[klass]||=klass.metaclass.ancestors.include?(FriendlyId)
@@friendly_support[klass] == true ? model_class.friendly.find(id) : model_class.find(id)
end
end
end
end
end