语法错误、意外的输入结束、期待结束——我的代码缺少一个 'end',我不知道为什么

syntax error, unexpected end-of-input, expecting end — my code is missing an 'end' and I cannot figure out why

错误

文本错误:

D:/Projects/Web/RoR/ecommerce/app/controllers/search_controller.rb:46: syntax error, unexpected end-of-input, expecting end

从屏幕截图中可以看出,程序期望(我认为)在最后一行之后有一个 'end';至少,添加额外的 '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

到底是怎么回事?

您正在用 do 呼叫 unless,并且 Ruby 期待每个 doend。这也不是 unless 语句的正确语法。

而不是

unless token.empty? do

写入

unless token.empty?

此外,您不应该使用 then in if statements