所以,我完全理解为什么人们通常会收到未定义的方法错误,但是,我不确定为什么会在这个特定时间发生。
我有一个部分:
提交/_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>
这也是仪表板 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
最佳答案
我认为您的错误将是模型级别的:
undefined method `submissions' for #<PlayerProject:0x000001052730e8>
协会
这通常表明您没有设置特定的关联(即,当 x
未定义时,您正在调用 @model.x
):
@sent_submissions = current_user.player_projects.collect{|ps| ps.submissions }.flatten
虽然您的 player_projects
已定义,但我认为您的 submissions
属性/关联不存在。此外,您还可以使用pluck
方法来帮助这个:current_user.player_projects.pluck(:submissions)
首先,您的表格中有submissions
属性吗?
其次,如果你不这样做,你想用它来实现什么目的?表中通常不会有复数列(表示多个数据存储)。我会在连接模型上使用关联
关于ruby-on-rails - 渲染部分时出现未定义方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23903765/