Ruby 腐烂土豆上的未定义局部变量或方法“id”
我一直在研究一个Edx问题,HOMEWRK 2已经有2个小时了。我正式被卡住了。你是怎么做的。这个问题与 我要做的是将电影标题链接到as sort,这样它就可以按顺序对电影进行排序Ruby 腐烂土豆上的未定义局部变量或方法“id”,ruby,Ruby,我一直在研究一个Edx问题,HOMEWRK 2已经有2个小时了。我正式被卡住了。你是怎么做的。这个问题与 我要做的是将电影标题链接到as sort,这样它就可以按顺序对电影进行排序 -# This file is app/views/movies/index.html.haml %h1 All Movies %table#movies %thead %tr %th= link_to 'Movie Title', new_movie_path %th Rat
-# This file is app/views/movies/index.html.haml
%h1 All Movies
%table#movies
%thead
%tr
%th= link_to 'Movie Title', new_movie_path
%th Rating
%th Release Date
%th More Info
%tbody
- @movies.each do |movie|
%tr
%td= movie.title
%td= movie.rating
%td= movie.release_date
%td= link_to "More about #{movie.title}", movie_path(movie)
= link_to 'Add new movie', new_movie_path
根据家庭作业,我应该编辑索引,以便将所有电影按顺序排列。我已经查找了ruby的排序方法,它是movie.order。我不知道该把什么放在括号里
class MoviesController < ApplicationController
def show
id = params[:id] # retrieve movie ID from URI route
@movie = Movie.find(id) # look up movie by unique ID
# will render app/views/movies/show.<extension> by default
end
def index
@movies = Movie.order(id)
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
因此,根本问题是,我不知道如何正确编辑方法索引以给出我的电影目录顺序,也不知道如何将标题电影标题分配给指向索引。您可以检查。索引函数中的变量id不存在。在视图中
%th= link_to 'Movie Title', movies_path(sort_param: 'title')
在控制器中
def index
@movies = Movie.order(params[:sort_param])
end
您也可以对其他标题(如“评级”、“发布日期”等)执行相同的操作。在您的模型中创建一个范围。这是最有建议的方法。控制器不需要知道如何检索结果。问问模特你需要什么 在“范围”中,按照您想要的方式进行设计。查看更多信息 在电影模型文件中,放置以下内容:
scope :ordered, order("id ASC")
然后在控制器索引中:
@movies = Movie.ordered
这会给你分类的电影集带来不起作用的我在MoviesControllerindex中发现了一个TypeError,它说你不能将符号转换成整数。Josh,这对应于哪一行?TypeError应该发生在某一行上-我们需要知道那一行上有什么代码来帮助你,所以我第二次尝试了它,它是有效的。非常感谢。你是一个救命恩人,我用rails链接订购已经有很长一段时间了。我似乎能正确地实施它