0

I am getting the error message "wrong number of arguments error(1 for 0) in my posts controller in my show action. I will comment the end of that specific line. Thanks for the help.

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
Jakob S
  • 19,575
  • 3
  • 40
  • 38
tomcruise
  • 17
  • 1
  • 7

2 Answers2

6

You'll want to read the the latest Rails Active Record Query Interface Guide. A lot has changed lately. The short answer is that you now chain together conditions. And .all does not take any arguments -- as the error message is telling you. Instead, you want to use the .order() method:

@posts = Post.order(created_at: :desc)
pdobb
  • 17,688
  • 5
  • 59
  • 74
  • I'm not sure why there's a downvote on this. It is correct that `.all` no longer takes any arguments in version 4.1. In the previous 4.0 version, I believe you could still use the old option parameters in `.all`, but no longer. – Paul Richter Aug 19 '14 at 20:26
2

You either have to show 1 post, and it will be:

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

or all posts

@posts = Post.order('created_at DESC') # index

Taking into account the fact, you write this in show action, you probably meant first.


Also small recommendation regarding strong parameters. Instead of writing this @post = Post.new(params[:post]) you would rather want to write in #create:

@post = Post.new(post_params) 

private

def post_params
  params.require(:post).permit(:title, :body) #whatsoever your post has
end
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145