我一直在开发应用程序,并且安装了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
ArticlesController
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 $1 [["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" = $1 [["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" = $1 AND "action_text_rich_texts"."record_type" = $2 AND "action_text_rich_texts"."name" = $3 LIMIT $4 [["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" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 1], ["record_type", "Article"], ["name", "article_image"], ["LIMIT", 1]]
(0.2ms) COMMIT
=> nil
我一直在开发应用程序,并且安装了friendly_id 5.3版,一切正常。但是,最近在该应用程序上工作时,我发现friendly_id在它一直使用的所有内容上都无法使用...
cancancan
宝石之间存在冲突。您需要在cancan.rb
中添加一个名为config/initializers
的文件,其内容如下:
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