我正在尝试在 Rails 上运行一个应用程序,用于 saas 课程,作业 2。每当我刷新页面时,都会出现以下错误:
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 个小时来尝试不同的东西。