Destroy方法不起作用,而是显示方法实现(Rails)

时间:2012-10-30 03:53:36

标签: ruby-on-rails methods

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

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

的routes.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 %>

这很好用

2 个答案:

答案 0 :(得分:3)

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

<%= link_to image_tag("/images/glossy_green_button.png"), device , :method => :delete,  :confirm => "Are you sure?"

答案 1 :(得分:0)

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

使用button_to(传递::方法:删除),并适当地设置按钮的样式。

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