Azketta
Azketta

Reputation: 79

Undefined Method Error When Rendering Partial

So, I entirely understand why one would normally receive an undefined method error, however, I am unsure why it is happening this particular time.

I have a partial:

submissions/_submission.html.erb

<div class="pure-u-1-2 rounded">
 <%= link_to submission do %>
  <div class="rounded" style="background-color: #B81324; color: #E6E6E6; margin: 10px; padding:5px;">
  <div class="clear">Project: <%= submission.project.title %></div>
  <div class="clear">Submitted By: <%= submission.submitter.username %></div>
  <div class="clear">Amount: $<%= submission.price %></div>
  <div class="clear">Description: <%= submission.description %></div>
  </div>
 <% end %>
</div>

This partial is used twice in my dashboard/index.html.erb Once to render a collection of received submissions and again to render a collection of sent submissions:

dashboards/index.html.erb

<% if @received_submissions.length > 0 %>
 <div class="pure-u-1" id="projects"><h3 class="red">Final Tracks Received (<%= @received_submissions.length %>)</h3>
  <%= render :partial => :submission, :collection => @received_submissions %>
 </div>
<% end %>

<% if @sent_submissions.length != 0 %>
 <div class="pure-u-1" id="projects"><h3 class="red">Final Tracks Sent (<%= @sent_submissions.length %>)</h3>
  <%= render :partial => :submission, :collection => @sent_submissions %>
 </div>
<% end %>

However, rendering the received submissions works without ailment, and rendering the sent submissions errors out with:

undefined method `submissions' for #<PlayerProject:0x000001052730e8>

Here is the dashboards_controller also, just in case:

class DashboardsController < ApplicationController
 before_filter :authenticate_user!

 def index
  @compact = current_user.projects.where.not(status: ['Deleted','Closed']).order("id desc")
  @projects = current_user.projects.where.not(status: ['Deleted','Closed']).order("id desc")
  @received_messages = current_user.received_messages
  @received_submissions = current_user.projects.collect{|p| p.submissions }.flatten

  @player = current_user.player_projects.collect{|pp| pp.project}
  @sent_submissions = current_user.player_projects.collect{|ps| ps.submissions }.flatten
 end
end

models/player_project.rb'

class PlayerProject < ActiveRecord::Base
  belongs_to :project
  belongs_to :player, :class_name => "User"
end

models/submission.rb

class Submission < ActiveRecord::Base
 belongs_to :submitter, :class_name => "User"
 belongs_to :recipient, :class_name => "User"
 belongs_to :project

 validates :amount_in_cents, :presence => true, length: { minimum: 2, :message => " must be more than $0" }
 validates :description, :presence => { :message => " cannot be blank" }
 validates :final_track_url, :presence => { :message => " Required" }

 def price
     sprintf "%.2f", (self.amount_in_cents.to_f/100.0)
 end

 def price=(val)
     self.amount_in_cents = (val.to_f*100.0).to_i
 end

 def paid?
     !!self.paypal_confirmation
 end

end

Upvotes: 2

Views: 1626

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

I think your error will be model-level:

undefined method `submissions' for #<PlayerProject:0x000001052730e8>

Association

This typically suggests you don't have a particular association set up (I.E you're calling @model.x when x is not defined):

@sent_submissions = current_user.player_projects.collect{|ps| ps.submissions }.flatten

Although your player_projects is defined, I think your submissions attribute / association does not exist. Besides, you can use the pluck method to help this: current_user.player_projects.pluck(:submissions)

Firstly, do you have the submissions attribute in your table?

Secondly, if you don't, what are you trying to achieve with it? You don't typically have a plural column in your table (denotes multiple data stores). I would use an association on a join-model

Upvotes: 2

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Try this

<%= render "submission", :collection => @sent_submissions %>

Upvotes: 1

Related Questions