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

时间:2018-10-12 01:34:33

标签: ruby-on-rails ruby ruby-on-rails-5 draper

我正在尝试为我的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 %>

1 个答案:

答案 0 :(得分:1)

我不熟悉这个装扮的宝石,但是您试图在看起来像对象集合而不是ActiveRecord对象上调用exists?。而且由于exists?defined in ActiveRecord,所以您将无法做到这一点。

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

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

收件人:

<% if @accounts.empty? then %>

.empty?方法是defined in multiple places,但是要点是,如果像集合这样的对象为空,则它将返回true。