首页 > 解决方案 > Rails 管理员在广告模型中不显示任何记录

问题描述

Rails 管理员不显示任何记录。即使有 102 条记录仍然存在。

在此处输入图像描述

甚至没有在仪表板中列出

在此处输入图像描述

但我可以看到一个广告

在此处输入图像描述

我的广告模型

class Ad < ApplicationRecord
  extend FriendlyId

  belongs_to :category
  belongs_to :user
  has_many :ad_images
  alias_attribute :images, :ad_images
  enum status: [:draft, :review, :published, :moderate]

  friendly_id :title, use: :slugged
  acts_as_paranoid
  after_initialize :set_default_status, :if => :new_record?

  scope :latest, -> { order(time_start: :asc) }
  scope :status_draft, -> { where(status: Ad.statuses[:draft]) }
  scope :status_published, -> { where(status: Ad.statuses[:published]) }
  scope :current_displayed, -> { where('time_start < ? AND time_end > ?', Time.now, Time.now) }

  scope :show_active, -> { status_published.latest.current_displayed }
  scope :show_draft, -> { status_draft.latest.current_displayed }

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

  def set_default_status
    self.status ||= :draft
  end

  def related
    category.ads.where.not(id: id).show_active.limit(6)
  end
end

我的 rails_admin.rb

RailsAdmin.config do |config|

  ### Popular gems integration

  ## == Devise ==
  config.authenticate_with do
    warden.authenticate! scope: :user
  end

  ## == method to call for current_user ==
  config.current_user_method(&:current_user)

  ## == CancanCan ==
  config.authorize_with :cancancan

  ## == Pundit ==
  # config.authorize_with :pundit

  ## == PaperTrail ==
  # config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0

  ### More at https://github.com/sferik/rails_admin/wiki/Base-configuration

  ## == Gravatar integration ==
  ## To disable Gravatar integration in Navigation Bar set to false
  # config.show_gravatar = true

  config.parent_controller = 'ApplicationController'

  config.actions do
    dashboard                     # mandatory
    index                         # mandatory
    new
    export
    bulk_delete
    show
    edit
    delete
    show_in_app

    ## With an audit adapter, you can add:
    # history_index
    # history_show
  end

  # config.model 'Ad' do
    # list do
      # field :title
      # field :created_at
    # end
  # end
end

cancancan能力文件

# frozen_string_literal: true

class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:
    #
    user ||= User.new # guest user (not logged in)
    if user.admin?
      can :access, :rails_admin
      can :manage, :all
    end
    #
    # The first argument to `can` is the action you are giving the user
    # permission to do.
    # If you pass :manage it will apply to every action. Other common actions
    # here are :read, :create, :update and :destroy.
    #
    # The second argument is the resource the user can perform the action on.
    # If you pass :all it will apply to every resource. Otherwise pass a Ruby
    # class of the resource.
    #
    # The third argument is an optional hash of conditions to further filter the
    # objects.
    # For example, here the user can only update published articles.
    #
    #   can :update, Article, :published => true
    #
    # See the wiki for details:
    # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
  end
end

我没有为 rails_admin 和 ad.rb 模型做任何花哨的配置设置。这发生在开发环境和生产环境中。我认为这是因为 rails_admin 隐藏了一些我不知道的东西。我不知道该怎么办。

标签: ruby-on-railsrubyrails-admin

解决方案


我通过将“广告”模型重命名为“项目”解决了这个问题


推荐阅读