My error message is "wrong number of arguments (0 for 1)"
我的错误消息是“错误的参数数量(0表示1)”
for this line: @post = Post.destroy in my
对于这一行:@post = Post.destroy in my
PostsController#destroy
I have a model which is post.rb
我有一个post.rb的模型
My Posts Controller is here
我的帖子控制器在这里
class PostsController < ApplicationController
def new
@post = Post.new
end
def index
@posts = Post.all
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def post_params
params.require(:post).permit(:title, :text)
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :text))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post = Post.destroy
redirect_to posts_path
end
end
In my view I have this code:
在我看来,我有这个代码:
<%= link_to 'Destroy', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %>
This is what it says I have for the parameters in the request
这就是我对请求中的参数所说的内容
{"_method"=>"delete",
"authenticity_token"=>"Pzsnxv8pt+34KIKpYqfZquDv3UpihkINGSJxomMNsW4=",
"id"=>"3"}
What in the heck am I doing wrong??
我做错了什么?
4 个解决方案
#1
11
You need to provide the ID
to the destroy
method:
您需要为destroy方法提供ID:
Post.destroy(params[:id])
As stated here: http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy
如上所述:http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy
#2
4
Here is your problem:
这是你的问题:
@post = Post.destroy
In Ruby you can destroy object in this way:
在Ruby中,您可以通过以下方式销毁对象:
@post.destroy
Another tip: when you are using variables just inside model or controller, declare them as locals by not adding @ in front of them and use @ just for variables that you need to use globally. Learn more about that here: In what circumstances should I use instance variables instead of other variable types?
另一个提示:当你在模型或控制器中使用变量时,通过不在它们前面添加@来将它们声明为locals,并将@ just用于需要全局使用的变量。在此处了解更多信息:在什么情况下我应该使用实例变量而不是其他变量类型?
#3
3
Precisely what the error says.
确切地说,错误是什么。
You can:
- Call
destroy
on an instance with no argument, e.g.,@post.destroy
- On the class with an id, e.g.,
Post.destroy(an_id)
在没有参数的实例上调用destroy,例如@ post.destroy
在具有id的类上,例如Post.destroy(an_id)
#4
2
I guess you wish to write:
我想你想写:
@post.destroy
#1
11
You need to provide the ID
to the destroy
method:
您需要为destroy方法提供ID:
Post.destroy(params[:id])
As stated here: http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy
如上所述:http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy
#2
4
Here is your problem:
这是你的问题:
@post = Post.destroy
In Ruby you can destroy object in this way:
在Ruby中,您可以通过以下方式销毁对象:
@post.destroy
Another tip: when you are using variables just inside model or controller, declare them as locals by not adding @ in front of them and use @ just for variables that you need to use globally. Learn more about that here: In what circumstances should I use instance variables instead of other variable types?
另一个提示:当你在模型或控制器中使用变量时,通过不在它们前面添加@来将它们声明为locals,并将@ just用于需要全局使用的变量。在此处了解更多信息:在什么情况下我应该使用实例变量而不是其他变量类型?
#3
3
Precisely what the error says.
确切地说,错误是什么。
You can:
- Call
destroy
on an instance with no argument, e.g.,@post.destroy
- On the class with an id, e.g.,
Post.destroy(an_id)
在没有参数的实例上调用destroy,例如@ post.destroy
在具有id的类上,例如Post.destroy(an_id)
#4
2
I guess you wish to write:
我想你想写:
@post.destroy