简体   繁体   English

如何在不使用link_to的情况下链接到Ruby-on-rails模板中的Delete Verb

[英]How to link to Delete Verb in Ruby-on-rails template without using link_to

My question is how to tell rails that it should be DELETE request and not GET. 我的问题是如何告诉Rails应该是DELETE请求而不是GET。 It is easy with link_to helper , but how to do this in this situation, as I need this link in given format. 使用link_to helper很容易,但是在这种情况下如何执行此操作,因为我需要使用给定格式的此链接。

Any suggestions ? 有什么建议么 ?

      <a class="item" role="button" tabindex="0" aria-label="Like" href="<%=  logout_path %>">
        <i class="fi-power"></i>
        <label id='itemlabel2'>Logout</label>
      </a>

So naturally, whenever one wants to send a delete request via the link in Rails, it normally would be done like this... 因此,自然而然地,每当要通过Rails中的链接发送删除请求时,通常都可以这样进行...

<%= link_to "Delete", @object_name, method: :delete %>

If we then have a look at what happens inside the browser, we will see the following... 然后,如果我们查看浏览器内部发生了什么,我们将看到以下内容...

<a rel="nofollow" data-method="delete" href="/object_name/1">Delete</a>

As you can see the key ingredient here is data-method="delete" , which initiates a POST request. 如您所见,这里的关键要素是data-method="delete" ,它启动POST请求。 Hence, my suggestion would be to convert your code to this... 因此,我的建议是将您的代码转换为此。

<a class="item" role="button" tabindex="0" data-method="delete" aria-label="Like" href="/logout">
  <i class="fi-power"></i>
  <label id='itemlabel2'>Logout</label>
</a>

Hopefully this might help you out. 希望这可以帮助您。

UPDATE: 更新:

Note that I have used href="/logout" above. 请注意,我在上面使用了href="/logout" I forgot to mention that this implies that inside of your routes.rb file you have to have a correct named route specified to that action, which in this case would be... 我忘了提到这意味着您的route.rb文件中必须为该操作指定正确的命名路由 ,在这种情况下,该路由应该是...

delete 'logout' => 'sessions#destroy'

...which in turn would trigger the destroy action inside of the SessionsController. ...这反过来会触发SessionsController内部的destroy动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM