加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 04:31:56 所属栏目:百科 来源:网络整理
导读:我的错误消息是“错误的参数数量(0表示1)” 对于这一行:@post = Post.destroy in my PostsController#destroy 我有一个post.rb的模型 我的帖子控制器在这里 class PostsController ApplicationController def new @post = Post.new end def index @posts =
我的错误消息是“错误的参数数量(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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读