Rails 我的文件中有语法问题?

Rails syntax problems in my file?

我不断收到这些错误:

/Users/macowner/workspace/blog/app/controllers/articles_controller.rb:3: syntax error, unexpected tIVAR, expecting keyword_end end @article = Article.all ^
/Users/macowner/workspace/blog/app/controllers/articles_controller.rb:7: syntax error, unexpected keyword_end, expecting ')'
/Users/macowner/workspace/blog/app/controllers/articles_controller.rb:28: syntax error, unexpected end-of-input, expecting keyword_end

我知道我的语法必须简单:

class ArticlesController < ApplicationController
    def index
    end @article = Article.all


    def show
        @article = Article.find(params(:id)
    end

    def new
        @article = Article.new
    end

    def create
        @article = Article.new(article_params)

        if @article.save
            redirect_to @article
        else render 'new'
    end

    private
    def article_params
        params.require(:article).permit(:title, :text)
    end

应该

def index
end @article = Article.all

成为

def index
    @article = Article.all
end

?

我在格式化你的 post 时觉得这有点奇怪。

此外,class 末尾缺少 end。还有一个失踪的父母 @article = Article.find(params(:id))

class ArticlesController < ApplicationController
    def index
    end @article = Article.all


    def show
        @article = Article.find(params(:id))
    end

    def new
        @article = Article.new
    end

    def create
        @article = Article.new(article_params)

        if @article.save
            redirect_to @article
        else render 'new'
    end

    private
    def article_params
        params.require(:article).permit(:title, :text)
    end
end

您可能需要考虑切换到编辑器或 IDE,它会突出显示并帮助您发现语法错误。

另请查看错误消息。文件名后的数字可帮助您找到发生错误的行。 (它通常是那一行或它之前的一行。)例如:articles_controller.rb:3 表示在第 3 行或第 3 行之前有错误。当您习惯了 Ruby 时,这可能会使调试更容易一些。

class ArticlesController < ApplicationController
  def index
    @article = Article.all
  end

  def show
    @article = Article.find(params(:id))
  end

  def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to @article
    else
      render 'new'
    end
  end

private
  def article_params
    params.require(:article).permit(:title, :text)
  end

end