I have looked at a fair dozen other SO posts and can't seem to find why this error message is popping up. I have a _template.html.erb partial for my workout model that then gets rendered for each workout on the workouts#indexpage.
When I try to access the workouts#index page I get an error saying undefined methodname' for nil:NilClasshighlighting the line with@workout.name` in my partial. The full partial is:
<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>
My workouts#index page uses this to render the partial:
<%= @workouts.each do |w| %>
<%= render "workouts/template" %>
<% end %>
My workout model is (I don't see how this would affect things, but just for completion's sake):
class Workout < ActiveRecord::Base
belongs_to :user
has_many :exercises
end
My workouts controller is:
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
Finally, I tried @workout.name first but got an undefined local variable or method 'workout' for #<#<Class:0x007fe1aa3ace68>:0x007fe1b4babdf8> error, so I don't think this will solve the problem.
Any help is appreciated!
