【问题标题】:Friendly_id not working version 5.3 rails 6Friendly_id 不工作版本 5.3 rails 6
【发布时间】:2020-06-14 18:04:14
【问题描述】:

我一直在开发一个应用程序,我安装了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(&amp;: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

【问题讨论】:

    标签: ruby-on-rails ruby friendly-id


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多