Rail3 button_to远程自定义操作

时间:2012-05-25 13:27:05

标签: ruby-on-rails-3

我想用自定义远程操作制作按钮 当我尝试

<%= button_to "something", {:controller => :updates, :action => :new}, {:remote => true} %>

它工作正常,但如果我改变:动作到我在控制器中定义的动作

<%= button_to "something", {:controller => :updates, :action => :destroy_all, :method => :delete}, {:remote => true} %>

表单中生成的路径错误

<form action="/assets?action=destroy_all&controller=updates&method=delete" class="button_to" data-remote="true" method="post">

在updates_controller中,我定义了:destroy_all

def destroy_all
    #some spaghetti code
end

我做错了什么?

2 个答案:

答案 0 :(得分:2)

查看API:method属于html_options,而不属于options

<%= button_to "something", {:controller => :updates, :action => :destroy_all}, {:remote => true, :method => :delete} %>

您还需要在路线文件中添加指向"updates#destroy_all"

的路线

答案 1 :(得分:0)

问题不在于更改方法名称。您没有正确传递选项。参见

action="/assets?action=destroy_all&controller=updates&method=delete"

我瘦了它不是你想要的。尝试

<%= button_to "smth", {:controller => :updates, :method => :destroy_all}, {:remote => true, :method => :delete} %>

<%= button_to "smth", '/updates/destroy_all', {:remote => true, :method => :delete} %>

小心使用方法delete =)