ruby-on-rails – Rails:.destroy错误的参数数量(0表示1)

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails:.destroy错误的参数数量(0表示1)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的错误消息是“错误的参数数量(0表示1)”

对于这一行:@post = Post.destroy in my

@H_403_4@PostsController#destroy

我有一个post.rb的模型

我的帖子控制器在这里

@H_403_4@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

在我看来,我有这个代码

@H_403_4@<%= link_to 'Destroy',post_path(post),method: :delete,data: { confirm: 'Are you sure?' } %>

这就是我对请求中的参数所说的内容

@H_403_4@{"_method"=>"delete","authenticity_token"=>"Pzsnxv8pt+34KIKpYqfZquDv3UpihkINGSJxomMNsW4=","id"=>"3"}

我做错了什么?

解决方法

您需要为destroy方法提供ID: @H_403_4@Post.destroy(params[:id])

如上所述:http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy

猜你在找的Ruby相关文章