簡體   English   中英

語法錯誤,意外的輸入結束,預期結束 - 我的代碼缺少“結束”,我不知道為什么

[英]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

從截圖中可以看出,程序在最后一行之后期待(我認為)一個“結束”; 至少,添加一個額外的“結束”會刪除錯誤消息。

但是,我就是不明白為什么。 更多的應該是不必要的。 我在這里錯過了顯而易見的事情嗎?

在這里提出這樣的問題讓我很傷心,但我要扯掉我的頭發了。

代碼

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 個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??
    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使用do並且 Ruby 期望每個do end 這也不是unless語句的正確語法。

代替

unless token.empty? do

unless token.empty?

此外,您不應在 if 語句中使用then

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM