我文件中的Rails语法问题?

时间:2015-08-08 20:47:47

标签: ruby-on-rails ruby ruby-on-rails-3

我一直收到这些错误:

/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

2 个答案:

答案 0 :(得分:4)

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

另请查看错误消息。文件名后面的数字可帮助您找到发生错误的行。 (它通常是该行或它之前的一行。)例如:articles_controller.rb: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

答案 1 :(得分:1)

应该

def index
end @article = Article.all

def index
    @article = Article.all
end

我认为在格式化帖子时有点奇怪。

另外,在课程结束时遗漏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