Ruby-on-rails rails 3 - link_to 销毁不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4606860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails 3 - link_to to destroy not working
提问by Alex
I am trying to create a destroy link to my users controller, I am also using devise.
我正在尝试创建一个指向我的用户控制器的销毁链接,我也在使用设计。
Here is my code -
这是我的代码 -
View
看法
<%= link_to 'Delete User?', child, :confirm => "Are you sure you want to delete #{child.full_name}?", :method => :delete, :class => "user-additional", :style => "font-size:12px;font-weight:normal;" %>
Controller
控制器
def destroy
if @user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to account_index_path }
format.xml { head :ok }
end
end
end
Routes
路线
devise_for :users
resources :users, :except => [:new]
The link translates to localhost:3000/users/10
该链接转换为 localhost:3000/users/10
When clicked this opens the users show instead of deleting them
当点击这打开用户显示而不是删除他们
Any ideas ?
有任何想法吗 ?
回答by Omar Qureshi
Destructive actions should be performed as a form submission - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist
破坏性操作应作为表单提交执行 - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist
use button_to(passing a :method => :delete) instead and style the button appropriately.
使用button_to(传递 a :method => :delete)并适当地设置按钮的样式。
回答by JayX
Actually I just had the exactly same problem yesterday
实际上我昨天刚刚遇到了完全相同的问题
Try this:
尝试这个:
<%= button_to "delete", your_object, :method=>:delete, :class=>:destroy %>
It works (for me at least)
它有效(至少对我而言)
回答by Florin
In case that you are using jQuery instead of Prototype, you are probably missing a javascript file.
如果您使用 jQuery 而不是 Prototype,您可能缺少一个 javascript 文件。
You can find details on how to add it to your project from the jquery-ujsGitHub page or from episode 205of the Railscasts.
您可以从jquery-ujsGitHub 页面或Railscasts 的第 205 集找到有关如何将其添加到您的项目的详细信息。
回答by DanSingerman
At a guess I think it is because in Rails 3, unobtrusive javascript is now used for functionality such as this (Rails 2 would output a bunch of nasty inline javascript for your code, Rails 3 puts the javascript in an external file, and uses HTML5 data-attributes to interact with that.)
我猜测是因为在 Rails 3 中,不显眼的 javascript 现在用于这样的功能(Rails 2 会为您的代码输出一堆讨厌的内联 javascript,Rails 3 将 javascript 放在外部文件中,并使用 HTML5data-与之交互的属性。)
To solve this you need to include <%= csrf_meta_tags %>in your page header to reference the external javascript. It also deals with XSS issues.
要解决此问题,您需要<%= csrf_meta_tags %>在页眉中包含以引用外部 javascript。它还处理 XSS 问题。
Some details here: Delete link sends "Get" instead of "Delete" in Rails 3 view
回答by chech
If you are using jQuery, make sure you have something like this:
如果您正在使用 jQuery,请确保您有这样的内容:
<script type="text/javascript">
// this allows jquery to be called along with scriptaculous and YUI without any conflicts
// the only difference is all jquery functions should be called with $j instead of $
// e.g. $jQ('#div_id').stuff instead of $('#div_id').stuff
var $jQ = jQuery.noConflict();
</script>
回答by Musaffa
- follow the steps in the installation part rails/jquery-ujs
- add <%= javascript_include_tag "application" %>in your layout file.
- 按照安装部分rails/jquery-ujs 中的步骤操作
- 在您的布局文件中添加<%= javascript_include_tag "application" %>。
回答by SureshCS
If you haven't included jquery and jquery-ujs in your app , the default link_to default coming with scaffold wont work!
如果你没有在你的应用中包含 jquery 和 jquery-ujs,默认的 link_to 默认自带的脚手架将不起作用!
I had the same issue.It got solved after including both these js!
我有同样的问题。在包含这两个 js 后它解决了!
回答by ReggieB
Also if you get this problem in production mode, it may be because you have not compiled the assets. See http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
此外,如果您在生产模式下遇到此问题,可能是因为您尚未编译资产。请参阅http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
回答by Deepak Kabbur
Worked for me with confirmation message.
对我来说有效并提供确认消息。
<%= button_to 'Destroy', {action: :destroy, id: version.id}, onclick: 'return confirm("Are you sure?")', method: :delete %>

