2016-05-17 78 views
0

我看了一大堆其他SO帖子,似乎無法找到爲什麼會彈出此錯誤消息。我有一個_template.html.erb部分用於我的workout模型,然後在workouts#index頁面上針對每項鍛鍊進行渲染。Ruby錯誤:未定義的方法`name'爲零:NilClass

當我嘗試訪問workouts#index頁面時,出現一條錯誤消息,說​​在我的部分中突出顯示@workout.name這一行。完整的部分是:

<div class="container col-sm-6 col-md-4"> 
    <div class="card hoverable"> 
    <div class="card-image"> 
     <div class="view overlay hm-white-slight z-depth-1"> 
      <%#= link_to workout.workout_img class: "img-responsive" alt: "" %> 
      <div class="mask waves-effect"></div> 
     </div> 
     <span class="card-title"><%= @workout.name %></span> 
    </div> 
    <div class="card-content"> 
     <p><strong>Workout Type: </strong><%= @workout.workout_type %></p> 
     <p><strong>Goal: </strong><%= @workout.teaser %></p> 
     <p><%= @workout.description %></p> 
    </div> 
    <div class="card-action"> 
     <a class="red-text"><%= @workout.video %></a> 
    </div> 
    </div> 
</div> 

workouts#index頁面使用它來渲染部分:

<%= @workouts.each do |w| %> 
    <%= render "workouts/template" %> 
    <% end %> 

我的鍛鍊模式是(我不認爲這會如何影響的事情,但只是爲了完整性緣故):

class Workout < ActiveRecord::Base 
    belongs_to :user 
    has_many :exercises 
end 

我的鍛鍊控制器是:

class WorkoutsController < ApplicationController 
def index 
    @workouts = Workout.all 
end 

def show 
    @workout = Workout.find(params[:id]) 
end 

def new 
    @workout = Workout.new 
end 

def create 
    @workout = Workout.new 
    @workout.name = params[:workout][:name] 
    @workout.workout_type = params[:workout][:workout_type] 
    @workout.teaser = params[:workout][:teaser] 
    @workout.description = params[:workout][:description] 
    @workout.video = params[:workout][:video] 
    @workout.difficulty = params[:workout][:difficulty] 
    @workout.trainer = params[:workout][:trainer] 
    @workout.user_id = params[:workout][:user_id] 

    if @workout.save 
    flash[:notice] = "Workout was saved successfully." 
    redirect_to @workout 
    else 
    flash.now[:alert] = "Error creating workout. Please try again." 
    render :new 
    end 
end 

def edit 
    @workout = Workout.find(params[:id]) 
end 

def update 
    @workout = Workout.find(params[:id]) 

    @workout.name = params[:workout][:name] 
    @workout.workout_type = params[:workout][:workout_type] 
    @workout.teaser = params[:workout][:teaser] 
    @workout.description = params[:workout][:description] 
    @workout.video = params[:workout][:video] 
    @workout.difficulty = params[:workout][:difficulty] 
    @workout.trainer = params[:workout][:trainer] 
    @workout.user_id = params[:workout][:user_id] 

    if @workout.save 
    flash[:notice] = "Workout was updated successfully." 
    redirect_to @workout 
    else 
    flash.now[:alert] = "Error saving workout. Please try again." 
    render :edit 
    end 
end 

def destroy 
    @workout = Workout.find(params[:id]) 

    if @workout.destroy 
    flash[:notice] = "\"#{@workout.name}\" was deleted successfully." 
    redirect_to action: :index 
    else 
    flash.now[:alert] = "There was an error deleting the workout." 
    render :show 
    end 
end 
end 

最後,我試着@workout.name第一次,但得到了undefined local variable or method 'workout' for #<#<Class:0x007fe1aa3ace68>:0x007fe1b4babdf8>錯誤,所以我不認爲這會解決問題。

任何幫助表示讚賞!

回答

1

您正在模板中使用@workout實例變量,但未設置。而不是試圖用一個實例變量,在調用模板中設置一個局部變量和使用:

在訓練/指數,做

<%= @workouts.each do |w| %> 
    <%= render "workouts/template", workout: w %> 
<% end %> 

workout替換所有@workout的部分。

+0

非常好!謝謝! – Liz