繁体   English   中英

使用 ruby​​ on rails 在单击按钮时从数据库中删除记录

[英]Delete record from database on clicking button using ruby on rails

我正在研究一种在用户按下“删除按钮”时从数据库中删除记录的方法问题是当我单击该按钮时,我想删除的包会从网站中删除,但是当我检查数据库时,我找到了记录是。

控制器:

def deletepackage(user_id,package_id)
  @package=Packages.find(:all, :condition=>{:id => package_id}, 
                                           :condition => {:sender_Id => user_id})
    if (@package!=nil && req.receivedByCarrier==false)
        @package.destroy
    elsif (@package!=nil && req.receivedByCarrier==true)
        @package.destroy
    end
    return;
  end

  def delete
    deletepackage(1,1)
    package.destroy
    redirect_to(:action => 'show') 
  end

  def show
    @create_package=Packages.find(params[:id ])
  end

Show.html.erb :

 <%=form_for(:package,:url{:action=>'create_packages/delete',
                             :id=>@create_package.id})do |f| %>
   <p>Are you sure you want to delete this package?</p>
   <%= f.submit "Delete" %>

显示.html.erb:

<%= link_to 'Delete', controller: 'your_controller_name', action: 'destroy', id: @package.id, method: :delete %>

控制器:

def show
  @package = Packages.find(params[:id])
end

def destroy
  Packages.find_by(id: params[:id], sender_Id: 1).destroy
  redirect_to :index
end

您无法从 destroy 重定向到“显示”操作,因为没有要显示的包(包已销毁)。

PS:

我认为,您需要学习一些 rails-tutorials,例如Michael Hartl's Tutorial

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM