0

我需要制作一个按钮来销毁对象。而不是破坏它显示字段

inex.html.erb

<%= link_to image_tag("/images/glossy_green_button.png"), device , { :html => { :method =>  :delete}, :controller => :devices, :action => 'destroy',:id => device.id, :onclick => 'return confirm(\'Are you sure?\');' }, :method => :turnon %>

devices_controller.rb

def destroy
 @device = Device.find(params[:id])
 @device.destroy

  respond_to do |format|
   format.html { render action: "destroy" }
   format.json { head :no_content }
  end
 end

路线.rb

device GET    /devices/:id(.:format)      devices#show
       PUT    /devices/:id(.:format)      devices#update
       DELETE /devices/:id(.:format)      devices#destroy

欣赏我做错了什么的任何想法。谢谢你 D

更新:

<%= button_to "Delete", device ,  :method => :delete %>

这很好用

4

2 回答 2

3

您使用的是哪个版本的 Rails?为什么你的link_to方法调用如此复杂?它可以简单地重写。尝试以下一种:

<%= link_to image_tag("/images/glossy_green_button.png"), device , :method => :delete,  :confirm => "Are you sure?"
于 2012-10-30T04:04:47.127 回答
0

破坏性行为应作为表单提交执行 - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist

请改用 button_to(传递 :delete 的 :method)并适当地设置按钮的样式。

或试试这个<%= button_to "delete", your_object, :method=>:delete, :class=>:destroy %>

于 2012-10-30T05:16:14.173 回答