【问题标题】:syntax error, unexpected end-of-input, expecting end — my code is missing an 'end' and I cannot figure out why语法错误,意外的输入结束,预期结束 - 我的代码缺少“结束”,我不知道为什么
【发布时间】: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


    【解决方案1】:

    您使用do 调用unless,而Ruby 期望每个do 都有一个end。这也不是 unless 语句的正确语法。

    代替

    unless token.empty? do
    

    unless token.empty?
    

    另外,你不应该使用then in if statements

    【讨论】:

    • 我不是用end 关闭了unlessdo 吗?不过我会删除do,看看是否需要(早上)。
    • do 关键字不是 unless 语法的一部分
    • @verified_tinker:“我不是关闭了除非结束吗?” – unless 没有 do。结果与条件用then 分开,而不是do。 (或表达式分隔符,例如; 或换行符。)do 开始传递给token.empty? 的块,因此您需要两个 ends,一个用于unless 和一个用于块。
    猜你喜欢
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多