Bitwise
Bitwise

Reputation: 8461

undefined method `decorate' for #<Assignment::ActiveRecord_AssociationRelation

I'm trying to implement my first decorator for my view. I'm running into an issue though. When I try to render the view I get the title error undefined method "decorate" for #<Assignment::ActiveRecord_AssociationRelation. I'm not sure what I'm supposed to do? Any help would be great. Here is my code.

Assignment decorator:

class AssignmentDecorator < Draper::Decorator
  delegate_all
  decorates :assignment

  def status
    if finished
      "Finished"
    else
      "Waiting"
    end
  end
end

Pages Controller:

class PagesController < ApplicationController
  before_action :verify_account!, only: :dashboard

  def home; end

  def dashboard
    @assignments = current_account.assignments.all.decorate
    @invitation = Invitation.new
  end
end

View:

    <% @assignments.each do |assignment| %>
      <tr class="assignment-rows">
        <td><%= link_to assignment.name, account_assignment_path(assignment) %></td>
        <td><%= assignment.assigned_workers %></td>
        <td><%= assignment.status %></td>
      </tr>
    <% end %>

Error message: enter image description here

Upvotes: 2

Views: 2332

Answers (2)

Matias Carpintini
Matias Carpintini

Reputation: 137

Before

@assignments = current_account.assignments.all.decorate

After

@assignments = current_account.assignments.decorate.all

Upvotes: 0

Taryn East
Taryn East

Reputation: 27747

Draper Docs: https://github.com/drapergem/draper

If you read the docs ;) and look at the decorate_collection section: https://github.com/drapergem/draper#collections

which states:

Note: In Rails 3, the .all method returns an array and not a query. Thus you cannot use the technique of Article.all.decorate in Rails 3. In Rails 4, .all returns a query so this techique would work fine.

So if you're using Rails 3 - you need to use decorate_collection... or you could (and probably should) upgrade to rails 4

Upvotes: 1

Related Questions