【发布时间】:2020-07-23 21:10:39
【问题描述】:
错误
文字错误:
D:/Projects/Web/RoR/ecommerce/app/controllers/search_controller.rb:46: syntax error, unexpected end-of-input, expecting end
从截图中可以看出,程序在最后一行之后期待(我认为)一个“结束”;至少,添加一个额外的“结束”会删除错误消息。
但是,我就是不明白为什么。更多的应该是不必要的。我在这里错过了显而易见的事情吗?
在这里提出这样的问题让我很伤心,但我快要把我的头发扯掉了。
代码
class SearchController < ApplicationController
def index
end
def search
all_books = Book.all
all_authors = Author.all
all_genres = Genre.all
@filtered_books = []
# perform the search
params[:search_items].split(',').each do |token|
token = token.downcase.strip
unless token.empty? do
# look in books
all_books.each do |book|
if book.title.downcase.include?(token) then
@filtered_books << book
end
end
# look in authors
all_authors.each do |author|
if author.full_name.downcase.include?(token) then
@filtered_books << author.books
end
end
# look in genres
all_genres.each do |genre|
if genre.name.downcase.include?(token) then
@filtered_books << genre.books
end
end
end # end for unless do
end # end for params do
# remove duplicates
@filtered_books = @filtered_books.uniq
render plain: @filtered_books
end
def show
end
end
尝试的解决方案
我安装了一个 Ruby linter VS Code 扩展,它告诉我的信息与屏幕上的错误消息相同,但按照它的建议,我在相同的“缩进级别”上留下了 2 个 ends:
class SearchController < ApplicationController
def index
end
def search
all_books = Book.all
all_authors = Author.all
all_genres = Genre.all
@filtered_books = []
# perform the search
params[:search_items].split(',').each do |token|
token = token.downcase.strip
unless token.empty? do
# look in books
all_books.each do |book|
if book.title.downcase.include?(token) then
@filtered_books << book
end
end
# look in authors
all_authors.each do |author|
if author.full_name.downcase.include?(token) then
@filtered_books << author.books
end
end
# look in genres
all_genres.each do |genre|
if genre.name.downcase.include?(token) then
@filtered_books << genre.books
end
end
end # end for unless do??
end # end for params do??
end
# remove duplicates
@filtered_books = @filtered_books.uniq
render plain: @filtered_books
end
def show
end
end
我尝试将“固定”代码(由 linter)通过美化器,希望我没有看到任何东西,但它会留下一团糟:
class SearchController < ApplicationController
def index
end
def search
all_books = Book.all
all_authors = Author.all
all_genres = Genre.all
@filtered_books = []
# perform the search
params[:search_items].split(',').each do |token|
token = token.downcase.strip
unless token.empty? do
# look in books
all_books.each do |book|
if book.title.downcase.include?(token) then
@filtered_books << book
end
end
# look in authors
all_authors.each do |author|
if author.full_name.downcase.include?(token) then
@filtered_books << author.books
end
end
# look in genres
all_genres.each do |genre|
if genre.name.downcase.include?(token) then
@filtered_books << genre.books
end
end
end # end for unless do??
end # end for params do??
end
# remove duplicates
@filtered_books = @filtered_books.uniq
render plain: @filtered_books
end
def show
end
end
到底发生了什么?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-6