在文档开头缩进在 rails4 中是非法的

Indenting at the beginning of the document is illegal in rails4

这是我的控制器文件

class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]

def index
    @posts = Post.all.order("order_by DESC")
end

def show
    @post = Post.find(params[:id])
end



private

def find_post
    @post = Post.find(params[:id])
end

def post_params
    params.require(:post).permit(:title, :content)
end
end

这是我的index.html.haml

- @posts.each do |post| %h2= post.title %p = time_ago_in_words(post.created_at)

*当我 运行 启动服务器时,它在此代码中显示 'Indenting at the beginning of the document is illegal'(- @posts.each do |post|)*

1) 删除代码和行首之间的白色 spaces 就可以了。

2) 你的缩进不一致。在整个文件中坚持一定数量的 space:它可以是 1 space、2 space、1 个标签等。