在show.html.erb

时间:2015-10-20 22:33:40

标签: ruby-on-rails ruby

我正在尝试创建一个类似系统的评论,用户可以在“决策”中添加“结果”。

现在我已经在'决定'的show.html.erb中呈现了表单和结果,但结果会产生以下错误:未定义的局部变量或#<#:0x007fc6046099e8>的方法`结果

我的代码:

控制器/ outcomes_controller.rb

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

模型/ outcome.rb

class Outcome < ActiveRecord::Base
  belongs_to :decision
end

模型/ decision.rb

class Decision < ActiveRecord::Base
  has_many :outcomes
end

决定/ 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 %>

任何人都可以通过解释为什么会出现这个错误以及我能做些什么来帮助我?

1 个答案:

答案 0 :(得分:2)

听起来您可能需要将变量作为参数传递给partial。当您尝试拨打outcomes.actual时,它不知道outcomes是什么。您需要将其作为局部变量传递:

<%= render "outcomes/outcome", locals: {outcomes: @decision.outcomes} %>

或者只是从@decision实例变量中获取它:

<强>结果/ _outcome.html.erb

<%= @decision.outcomes.actual %>