Ruby on rails Rails:参数数目错误(给定1,应为0)

Ruby on rails Rails:参数数目错误(给定1,应为0),ruby-on-rails,ruby,arguments,ruby-on-rails-5,Ruby On Rails,Ruby,Arguments,Ruby On Rails 5,我在我的帖子索引页上发现此错误: 这就是模型: class Post < ApplicationRecord include Filterable belongs_to :region belongs_to :category belongs_to :topic validates :title, presence: true, length: { maximum: 500 } validates :content, presence: true vali

我在我的
帖子
索引页上发现此错误:

这就是模型:

class Post < ApplicationRecord

  include Filterable

  belongs_to :region
  belongs_to :category
  belongs_to :topic
  validates :title, presence: true, length: { maximum: 500 }
  validates :content, presence: true
  validates :published_at, presence: true
  translates :title, :content, :slug, touch: true, fallbacks_for_empty_translations: true
  has_attached_file :image, styles: { thumb: "100x70#", featured: "1560x868#", small: "760x868#", big: ">1600x1600" }
  validates_attachment :image, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
  validates_attachment_presence :image

  scope :published, -> (published) { where(published: (['true', true].include? published)).order(featured: :desc, published_at: :desc) }
  scope :published_until_now, -> { where("published_at < ?", Time.now).merge(Post.published(true)) }
  scope :topic, -> (topic_id) {
    joins(:topic).where('topic_id = ?', topic_id) }
  scope :category, -> (post_category) {
    joins(:category).where('category_id = ?', post_category) }
  scope :match, -> (search_term) {
    with_translations(I18n.locale).where('content like ? or title like ?', "%#{search_term}%", "%#{search_term}%") }

  self.per_page = 10

  after_save :unfeature_older_posts, if: Proc.new { |post| post.featured? }

  extend FriendlyId
  friendly_id :title, use: :globalize

  def unfeature_older_posts
    featured_posts = Post.where(featured: true).where.not(id: id).order(published_at: :desc)
    if featured_posts.size == 1
      featured_posts.last.update(featured: false)
    end
  end

end

我不知道接下来该怎么办。我最近升级到了RubyonRails 5和Ruby2.7.0,我不知道这是否相关。

尝试用
类方法替换
模块类方法

如果有效,请记住:


filter
方法来自Ruby。它在
数组中定义。正如您在
数组
上的
过滤器
方法中所看到的,该方法不带任何参数。这是您看到的错误的直接原因


在Rails中,当在
ActiveRecord
对象上调用
Array
上的方法时(在您的例子中是
Post.published\u now
),并且在模型上找不到方法时,它会自动将自身转换为
数组
。因此,它调用
Array
上的
filter
方法。通常情况下,您不希望定义诸如
filter
之类的方法,这会造成混乱。

尝试将
模块类方法
替换为
类方法

如果有效,请记住:


filter
方法来自Ruby。它在
数组中定义。正如您在
数组
上的
过滤器
方法中所看到的,该方法不带任何参数。这是您看到的错误的直接原因


在Rails中,当在
ActiveRecord
对象上调用
Array
上的方法时(在您的例子中是
Post.published\u now
),并且在模型上找不到方法时,它会自动将自身转换为
数组
。因此,它调用
Array
上的
filter
方法。一般来说,你不想定义一些方法,比如
filter
,这会让人感到困惑。

你能给我们看你的Post模型文件吗?@gabrimac我通过添加模型更新了问题。你能给我们看你的Post模型文件吗?@gabrimac我通过添加模型更新了问题。替换
模块ClassMethods
也给我一个错误,但是你把我放在了正确的轨道上,我把
filter
方法重命名为
filter\u posts
,它现在可以工作了。替换
module ClassMethods
也给了我一个错误,但是你把我放在了正确的轨道上,我把
filter
方法重命名为
filter\u posts
,它现在可以工作了。
class PostsController < ApplicationController

  before_action :get_pages_tree, :get_privacy_policy, only: [:index, :show]

  def index
    @filters = params.slice(:topic, :category)
    @posts = Post.published_until_now
      .filter(@filters)
      .paginate(:page => params[:page], per_page: 11)
  end

  def show
    @post = Post.friendly.find(params[:id])
  end
end
module Filterable
  extend ActiveSupport::Concern

  module ClassMethods
    def filter(filtering_params)
      results = self.where(nil)
      filtering_params.each do |key, value|
        results = results.public_send(key, value) if value.present?
      end
      results
    end
  end
end