所以,我完全理解为什么人们通常会收到一个未定义的方法错误,但是,我不确定为什么会在这个特定的时间发生。
我有一个部分:
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>
此部分在我的仪表板/ 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
答案 0 :(得分:2)
我认为您的错误将是模型级别的:
undefined method `submissions' for #<PlayerProject:0x000001052730e8>
<强>协会强>
这通常表示您没有设置特定的关联(当您未定义@model.x
时,您正在调用x
):
@sent_submissions = current_user.player_projects.collect{|ps| ps.submissions }.flatten
虽然您的player_projects
已定义,但我认为您的submissions
属性/关联不存在。此外,您可以使用pluck
方法来帮助:current_user.player_projects.pluck(:submissions)
首先,您的表中是否有submissions
属性?
其次,如果你不这样做,你想用它做什么?您的表中通常没有多列(表示多个数据存储)。我会在连接模型上使用关联
答案 1 :(得分:1)
试试这个
<%= render "submission", :collection => @sent_submissions %>