我的错误消息是“错误的参数数量(0表示1)”
对于这一行:@post = Post.destroy in my
PostsController#destroy
我有一个post.rb的模型
我的帖子控制器在这里
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
在我看来,我有这个代码:
<%= link_to 'Destroy',post_path(post),method: :delete,data: { confirm: 'Are you sure?' } %>
这就是我对请求中的参数所说的内容
{"_method"=>"delete","authenticity_token"=>"Pzsnxv8pt+34KIKpYqfZquDv3UpihkINGSJxomMNsW4=","id"=>"3"}
我做错了什么?
解决方法
您需要为destroy方法提供ID:
Post.destroy(params[:id])
如上所述:http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。