2015-10-20 53 views
1

我試圖創建一個類似系統的評論,其中用戶可以向「決定」添加「結果」。在show.html.erb中渲染時未定義的局部變量或方法

現在我已經呈現的形式和的「決定」,但成果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 %> 

任何人都可以通過解釋爲什麼發生此錯誤以及我能做些什麼來幫助我解決問題嗎?

+0

哪行代碼是你得到的錯誤? – Huy

+0

這可能是你需要將你的變量傳遞給你的部分。 – Huy

+0

@Huy錯誤發生在results/_outcome.html.erb文件中<%= outcomes.actual%>上。你有什麼建議我可以將變量傳遞給部分? – Hiltsje

回答

2

這聽起來像你可能需要將你的變量作爲參數傳遞給你的部分。當您嘗試撥打outcomes.actual時,它不知道outcomes是什麼。你要麼需要把它作爲一個局部變量:

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

或者只是簡單地從@decision實例變量得到它:

結果/ _outcome.html.erb

<%= @decision.outcomes.actual %>

相關問題