Posts控制器,rails 4中的参数错误数量为1​​(0为0)?

tom*_*ise 0 ruby ruby-on-rails ruby-on-rails-4 ruby-2.1

我在我的显示操作中的帖子控制器中收到错误消息"错误的参数数量错误(1为0).我将评论该特定行的结尾.感谢您的帮助.

def show
  @post = Post.all(:order => 'created_at DESC') #this is the error line
end

def new
  @post = Post.new
end

def create
  @post = Post.new(params[:post])

  if @post.save
    redirect_to @post
  else
   render :new
  end
end
Run Code Online (Sandbox Code Playgroud)

pdo*_*obb 6

您需要阅读最新的Rails Active Record Query Interface Guide.最近发生了很多变化.简短的回答是,你现在将条件联系在一起.并且.all不接受任何参数 - 正如错误消息告诉您的那样.相反,您想要使用该.order()方法:

@posts = Post.order(created_at: :desc)
Run Code Online (Sandbox Code Playgroud)