我正在尝试在Rails上运行一款面向SaaS课程的应用程序,在完成第二项作业时,每当我刷新页面都会出现以下错误:
NoMethodError in MoviesController#index
undefined method `keys' for nil:NilClass
Rails.root: C:/Sites/RailsProjects/hw2_rottenpotatoes
Application Trace | Framework Trace | Full Trace
app/controllers/movies_controller.rb:24:in `block in index'
app/controllers/movies_controller.rb:23:in `each'
app/controllers/movies_controller.rb:23:in `index'
我的movies_controller.rb
文件:
class MoviesController < ApplicationController
def show
id = params[:id] # retrieve movie ID from URI route
@movie = Movie.find(id) # look up movie by unique ID
end
def index
redirect = false
if params[:sort]
@sorting = params[:sort]
elsif session[:sort]
@sorting = session[:sort]
redirect = true
end
if redirect
redirect_to movies_path(:sort => @sorting, :ratings => @ratings)
end
Movie.find(:all, :order => @sorting ? @sorting : :id).each do |mv|
if @ratings.keys.include? mv[:rating]
(@movies ||= [ ]) << mv
end
end
session[:sort] = @sorting
session[:ratings] = @ratings
end
def new
# default: render 'new' template
end
def create
@movie = Movie.create!(params[:movie])
flash[:notice] = "#{@movie.title} was successfully created."
redirect_to movies_path
end
def edit
@movie = Movie.find params[:id]
end
def update
@movie = Movie.find params[:id]
@movie.update_attributes!(params[:movie])
flash[:notice] = "#{@movie.title} was successfully updated."
redirect_to movie_path(@movie)
end
def destroy
@movie = Movie.find(params[:id])
@movie.destroy
flash[:notice] = "Movie '#{@movie.title}' deleted."
redirect_to movies_path
end
end
我对Rails非常陌生,在尝试了多种方法后,已经被这个问题困扰了4个小时。