简体   繁体   English

Rails中的GET,POST,PUT,DELETE请求出现问题

[英]Issue with GET, POST, PUT, DELETE requests in Rails

I've been encountering a strange issue where all of my links, even if I specify the request type, still sends GET requests. 我一直遇到一个奇怪的问题,即使我指定了请求类型,我的所有链接仍会发送GET请求。 For example: 例如:

<%= link_to "<i class='fa fa-trash-o'></i>".html_safe, workout_path(workout), method: :delete, data: { confirm: 'Are you sure you want to delete this workout?' }, :class => "delete-workout" %>

Will send a GET request and direct me to the workout show page, rather than delete it. 将发送GET请求并将我定向到锻炼显示页面,而不是将其删除。

<%= link_to "Like", '/wellness/4/like',  method: :post, remote: true  %>

Does the same thing, sends a GET request, takes me to a show page with ID of 4, and I get the message: 做同样的事情,发送GET请求,将我带到ID为4的显示页面,我得到以下消息:

No route matches [GET] "/wellness/3/like" 没有路线符合[GET]“ / wellness / 3 / like”

which is correct since my route is: 这是正确的,因为我的路线是:

post "/wellness/:id/like" => "wellness#like"

I do have jQuery ujs loading correctly so that isn't the issue as well. 我确实正确加载了jQuery ujs,所以这也不是问题。

It sounds like Javascript is disabled in your browser. 听起来您的浏览器禁用了Javascript。 The Rails documentation for link_to says: 用于link_toRails文档说:

Note that if the user has JavaScript disabled, the request will fall back to using GET. 请注意,如果用户禁用了JavaScript,则该请求将退回到使用GET的状态。

One way to verify is to change your link_to to button_to , which is a safer choice, as explained in the Rails documentation for button_to : 验证的一种方法是将link_to更改为button_to ,这是一个更安全的选择,如关于button_toRails文档所述

Generates a form containing a single button that submits to the URL created by the set of options. 生成包含一个按钮的表单,该按钮提交到由选项集创建的URL。 This is the safest method to ensure links that cause changes to your data are not triggered by search bots or accelerators. 这是确保搜索引擎或加速器不会触发导致数据更改的链接的最安全方法。

In general, it's best not to rely on JavaScript for major functionality because it's not guaranteed that your users will have it turned on. 通常,最好不要依赖JavaScript的主要功能,因为不能保证您的用户将其打开。 Implementing a major feature like deleting and liking that only works if JS is turned on is not considered a best practice. 实施仅在JS启用时才起作用的主要功能(如删除和喜欢),不被视为最佳实践。 Instead, choose progressive enhancement or graceful degradation . 相反,请选择渐进增强或适度降级

I think that error because resourses. 我认为该错误是因为资源不足。

  • Maybe you need. 也许你需要。
 resourses wellness do resourses likes end 

- Then run # rake routes you can see uri: -然后运行#条耙路,您可以看到uri:

GET method /wellness/:id/likes

  • Hope this helps! 希望这可以帮助!

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

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