【发布时间】:2014-05-28 05:55:29
【问题描述】:
所以,我完全理解为什么通常会收到未定义的方法错误,但是,我不确定为什么会在这个特定时间发生。
我有一个部分:
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>
这个部分在我的dashboard/index.html.erb中被使用了两次 一次呈现收到的提交的集合,再次呈现已发送的提交的集合:
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 %>
但是,渲染收到的提交可以正常工作,并且渲染发送的提交错误:
undefined method `submissions' for #<PlayerProject:0x000001052730e8>
这也是dashboards_controller,以防万一:
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
【问题讨论】:
-
您的
User模型中有has_many :player_projects关联吗? -
是的,我其实有
has_many :player_projects, :foreign_key => 'player_id'
标签: ruby-on-rails ruby arrays rendering partials