Ruby on rails 在show.html.erb中呈现时未定义的局部变量或方法
我正在尝试创建一个类似于评论的系统,用户可以在“决策”中添加“结果” 现在,我已经在show.html.erb的'decisions'中呈现了表单和结果,但是结果给出了以下错误:未定义的局部变量或方法'outlets'# 我的代码: 控制器/U控制器.rbRuby on rails 在show.html.erb中呈现时未定义的局部变量或方法,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在尝试创建一个类似于评论的系统,用户可以在“决策”中添加“结果” 现在,我已经在show.html.erb的'decisions'中呈现了表单和结果,但是结果给出了以下错误:未定义的局部变量或方法'outlets'# 我的代码: 控制器/U控制器.rb class OutcomesController < ApplicationController def create @decision = Decision.find(params[:decision_id]
class OutcomesController < ApplicationController
def create
@decision = Decision.find(params[:decision_id])
@outcome = @decision.outcomes.create(params[:outcome].permit(:actual, :strength, :weakness))
redirect_to decision_path(@decision)
end
end
类结果控制器
模型/结果.rb
class Outcome < ActiveRecord::Base
belongs_to :decision
end
类结果
模型/决策.rb
class Decision < ActiveRecord::Base
has_many :outcomes
end
类决策
decisions/show.html.erb
<h1>Decision showpage</h1>
<h2><%= @decision.title %></h2>
<p><%= @decision.created_at %></p>
<p><%= @decision.forecast %></p>
<p><%= @decision.review_date %></p>
<%= render @decision.outcomes %>
<%= link_to "Delete Decision", decision_path(@decision), method: :delete, data: { confirm: "Are you sure?" } %>
<%= render "outcomes/form" %>
<%= render "outcomes/outcome" %>
决策显示页面
结果/_form.html.erb
<%= form_for([@decision, @decision.outcomes.build]) do |f| %>
<%= f.label :actual %>:
<%= f.text_field :actual %> <br/>
<%= f.label :strength %>:
<%= f.text_area :strength %> <br/>
<%= f.label :weakness %>:
<%= f.text_area :weakness %> <br/>
<%= f.submit %>
<% end %>
:
:
:
结果/_outcome.html.erb
<%= outcomes.actual %>
<%= outcomes.strength %>
<%= outcomes.weakness %>
有人能帮我解释一下为什么会发生此错误,以及我能做些什么来让它工作吗?听起来您可能需要将变量作为参数传递给分部函数。当您尝试调用
结果.actual
时,它不知道结果是什么。您需要将其作为局部变量传递:
或者直接从@decision
实例变量中获取:
结果/\u outcome.html.erb
您在哪一行代码上得到错误?可能是您需要将变量传递给您的分部代码。@Huy错误出现在outcouts/\u outcout.html.erb文件中。你有没有建议我如何将变量传递给部分变量?我更新了答案。希望能有帮助。@Huy我把它改成了,我相信它是有效的,但现在我得到了以下错误:未定义的方法'actual'用于#