繁体   English   中英

Ruby错误:nil:NilClass的未定义方法“名称”

[英]Ruby Error: Undefined Method `name' for nil:NilClass

我看了很多其他的SO帖子,但似乎找不到为什么出现此错误消息。 我的workout模型有一个_template.html.erb部分,然后在_template.html.erb workouts#index页面上为每种锻炼呈现了该部分。

当我尝试访问workouts#index页面时,出现错误undefined method 'name' for nil:NilClass突出显示了我的部分中带有@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>错误得到了一个undefined local variable or method 'workout' for #<#<Class:0x007fe1aa3ace68>:0x007fe1b4babdf8> ,所以我认为这不会解决问题。

任何帮助表示赞赏!

您在模板中使用@workout实例变量,但未设置。 与其尝试使用实例变量,不如在调用模板中设置局部变量并使用该变量:

在锻炼/索引中,

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

并取代所有的@workout在局部workout

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM