gpt4 book ai didi

ruby-on-rails - 渲染部分时未定义的方法错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:28 25 4
gpt4 key购买 nike

所以,我完全理解为什么人们通常会收到未定义的方法错误,但是,我不确定为什么会在这个特定时间发生。

我有一部分:

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

最佳答案

我认为你的错误将是模型级别的:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com