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

已验证_tinker

错误

错误图片

文字错误:

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 扩展,它告诉我与屏幕上的错误消息相同的事情,但遵循它的建议让我end在相同的“缩进级别”上有 2秒:

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

到底是怎么回事?

维克多

unless用 a调用doRuby 期望end每个do. 这也不是unless语句的正确语法

代替

unless token.empty? do

unless token.empty?

此外,您不应在 if 语句中使用then

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

$ .ajax()返回语法错误:json输入意外结束

json解析错误语法错误输入意外结束

错误“未捕获的语法错误:输入的意外结束”

错误:语法错误:JSON输入意外结束

语法错误“语法错误,意外的输入结束,需要keyword_end(SyntaxError)”

For循环不会结束。不知道为什么

我不知道为什么这段代码中有语法错误

bash:语法错误:文件意外结束

语法错误:意外的输入结束

我不知道为什么我的代码是错误的?那是什么错呢?

.bashrc语法错误:文件意外结束

.bashrc:语法错误:文件意外结束

语法错误:意外的keyword_ensure,预期输入结束

不知道为什么我收到 StopIteration 错误

SMTP 错误,我不知道为什么

For Each 循环提前结束,我不知道为什么

未捕获的语法错误:JSON 输入意外结束 [}

Ruby on Rails 语法错误,意外的输入结束,期待关键字结束 ^

我不知道为什么我在 jsp 文件中收到这个简单代码的错误

Laravel Swagger - 未捕获的语法错误:输入意外结束

我使用 Flask 在 python 中遇到语法错误,我不知道为什么

AWK - 语法错误:文件意外结束

未捕获的语法错误:输入行意外结束:78

语法错误,意外=>,期待结束

Ruby - 语法错误,意外的 tIDENTIFIER,期待结束

语法无效,我不知道为什么

我的代码给了我一个元组错误,我不知道为什么

Dockerfile 错误的目录。我不知道为什么

FatalErrorException:语法错误,文件意外结束