1

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

未定义的方法“存在吗?” 对于#Draper::CollectionDecorator:0x000055b625da7a10>

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


模型:Account.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

查看:accounts/index.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>

查看部分:accounts/_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 %>
4

1 回答 1

1

我不熟悉 draper gem,但您试图调用exists?看起来像对象集合的东西,而不是 ActiveRecord 对象。而且由于在 ActiveRecordexists?中定义,您将无法做到这一点。

您可能可以安全地更改此行:

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

至:

<% if @accounts.empty? then %>

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

于 2018-10-12T01:41:40.143 回答