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.
我在我的显示操作中的帖子控制器中收到错误消息“错误的参数数量错误(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
2 个解决方案
#1
2
You either have to show 1 post, and it will be:
您要么必须显示1个帖子,它将是:
@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.
考虑到这个事实,你在show动作中写这个,你可能首先想的。
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(params [:post])你宁愿写#create:
@post = Post.new(post_params)
private
def post_params
params.require(:post).permit(:title, :body) #whatsoever your post has
end
#2
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:
您需要阅读最新的Rails Active Record Query Interface Guide。最近发生了很多变化。简短的回答是,您现在将条件链接在一起。并且.all不接受任何参数 - 正如错误消息告诉你的那样。相反,您想要使用.order()方法:
@posts = Post.order(created_at: :desc)
#1
2
You either have to show 1 post, and it will be:
您要么必须显示1个帖子,它将是:
@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.
考虑到这个事实,你在show动作中写这个,你可能首先想的。
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(params [:post])你宁愿写#create:
@post = Post.new(post_params)
private
def post_params
params.require(:post).permit(:title, :body) #whatsoever your post has
end
#2
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:
您需要阅读最新的Rails Active Record Query Interface Guide。最近发生了很多变化。简短的回答是,您现在将条件链接在一起。并且.all不接受任何参数 - 正如错误消息告诉你的那样。相反,您想要使用.order()方法:
@posts = Post.order(created_at: :desc)