是否存在未定义的方法? for Draper::CollectionDecorator



我正在尝试为我的Rails项目实现Draper gem,但无法使其工作。 我收到错误消息:

未定义的方法"存在?",用于 #Draper::CollectionDecorator:0x000055b625da7a10>

该错误似乎是来自我在索引视图上呈现的部分。 部分正在调用"@accounts.exist?"这是发生错误的地方。 如果我删除对部分的调用,我的装饰器工作正常。 我有一种感觉,我需要为此使用委托命令,但我不确定。 我对 Rails 相当陌生,刚刚开始探索一些有用的宝石。我希望有人可以看看我的代码,并为我指出正确的方向,以克服这个错误。谢谢!:)


型号: 帐户.rb

# An account which will store many transactions and belongs to one user
class Account < ApplicationRecord
belongs_to :user
has_many :transactions, dependent: :destroy
validates :name, presence: true, length: { maximum: 50, minimum: 2 }
validates :starting_balance, presence: true
validates :last_four, length: { maximum: 4 }
# validates_associated :transactions
after_create :create_initial_transaction
def create_initial_transaction
update(current_balance: 0.00)
# rubocop:disable Metrics/LineLength
Transaction.create(trx_type: 'credit', trx_date: Time.current, account_id: id, description: 'Starting Balance', amount: starting_balance)
# rubocop:enable Metrics/LineLength
end
end

控制器:accounts_controller.rb

class AccountsController < ApplicationController
before_action :authenticate_user!
before_action :set_account, only: %i[show edit update destroy activate deactivate]
# GET /accounts
# GET /accounts.json
def index
@accounts = current_user.accounts.where(active: true).order('created_at ASC')
@accounts = @accounts.decorate
end
def inactive
@accounts = current_user.accounts.where(active: false).order('created_at ASC')
end
private
# Use callbacks to share common setup or constraints between actions.
def set_account
@account = current_user.accounts.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def account_params
params.require(:account).permit(:name, :starting_balance, :current_balance, :last_four, :active, :user_id)
end
end

装饰器: account_decorator.rb

class AccountDecorator < Draper::Decorator
decorates_finders
decorates_association :user
delegate_all
def last_four_formatted
last_four.present? ? ' ( ... ' + last_four.to_s + ')' : nil
end
end

查看: 帐户/索引.html.erb

<%= render partial: "shared/pagetitle", locals: { page_model: "account", description: "Accounts" } %>
<!-- Render the message if no accounts exist -->
<%= render partial: "accounts/noaccounts", locals: { description: "It looks like you don't have any accounts added. To add an account, click the add account button at the top of the page :)" } %>
<div class="row" id="accounts">
<%= render partial: "accounts/account", collection: @accounts %>
</div>

查看部分: 帐户/_noaccounts.html.erb

<% if !@accounts.exists? then %>
<div class="row">
<div class="col s12 m8 offset-m2">
<div class="card ob-card-gradient ob-account-card">
<div class="card-content ob-text-primary">
<span class="card-title">Welcome to mysite!</span>
<div class="row">
<br />
<div class="col s1"><i class="material-icons valign-wrapper">account_balance</i></div>
<div class="col s11">
<%= description %></div>
</div>
</div>
</div>
</div>
</div>
<% end %>

我不熟悉 draper gem,但您正在尝试在看起来像对象集合的内容上调用exists?,而不是在 ActiveRecord 对象上调用。由于exists?是在ActiveRecord中定义的,因此您将无法执行此操作。

您可以安全地更改此行:

<% if !@accounts.exists? then %>

自:

<% if @accounts.empty? then %>

.empty?方法在多个地方定义,但它的要点是,如果像集合这样的对象为空,那么它将返回 true。

最新更新