我正在使用Rails 3,并且有一个页面可以输出数据库中的帖子列表.我希望能够从链接中删除帖子.
下面的第二个例子有效,但第一个没有.谁知道为什么第一个不起作用?我的观点包含:
# this link sends a "GET" request which asks for the #show function
<%= link_to 'Delete', post, :method => :delete %>
# this link sends the proper "DELETE" request which asks for the #destroy function
<%= button_to 'Delete', post, :method => :delete %>
Run Code Online (Sandbox Code Playgroud)
我的路线文件包含以下内容:
resources :posts
Run Code Online (Sandbox Code Playgroud) 我正在使用Rails书进行敏捷Web开发,但我一直在使用Twitter Bootstrap而不是书中的自定义样式.我无法通过GLyphonics向button_to方法添加图标.我的代码看起来像这样:
<%= button_to <i class="icon-search icon-white">Add To Cart</i>,
line_items_path(product_id: product),
class: "btn btn-success" %>
Run Code Online (Sandbox Code Playgroud)
我尝试了很多变化,但似乎无法让它正常工作.
我建立此链接是为了销毁评论:
<%= link_to 'Destroy Comment', [comment.post, comment],
:confirm => 'Are you sure?', :method => :delete %>
Run Code Online (Sandbox Code Playgroud)
这假设要发送到comments_controller中的destroy动作.
问题是它搜索'show'动作,而不是'destroy'动作:
Unknown action
The action 'show' could not be found for CommentsController
Run Code Online (Sandbox Code Playgroud)
你认为你知道为什么会这样吗?
谢谢,
俄德
编辑:问题解决了我用'button_to'
我在Rails中有一个删除链接:
<%= link_to "#{image_tag("icons/icon_delete.png", :alt => "Delete")} Delete".html_safe, "#suppliers", id: supplier.id, class: "delete", :title => "Delete", :confirm => 'Are you sure?', :method => :delete %>
Run Code Online (Sandbox Code Playgroud)
我有一个捕获它的骨干事件(CofeeScript):
events: ->
"click a.delete": "deleteSupplier"
deleteSupplier: (event) ->
event.preventDefault()
console.log("delete")
Run Code Online (Sandbox Code Playgroud)
所有这些都可以正常工作,只有当我在确认对话框中单击"确定"时,才会跟踪链接.任何想法如何防止这种情况?如果我从deleteSupplier返回false,则确认不再有效,这不是我所追求的.我希望确认工作正常,当我点击确定时,不要通过链接.
编辑:我忘了提到在通过ajax get请求加载页面后插入这些链接.我还通过删除:确认测试,它似乎没有任何区别.我相信它与通过ajax请求加载有关,但我认为如果是这样的话,事件甚至不会触发:(