【问题标题】::method put: not working instead Get method:method put: 不工作而不是 Get 方法
【发布时间】:2016-09-23 10:08:19
【问题描述】:

所以我正在尝试使用以下路线:

<%= link_to like_post_path(@post), :method => :put do %>

但我不知道为什么使用GET方法而不是PUT

 No route matches [GET] "/posts/1/like"

对我来说毫无意义..

myroutes.rb:

 resources :posts do
    member do
        put "like" => "posts#upvote"
        put "dislike" => "posts#downvote"
    end

【问题讨论】:

  • 尝试使用button_to而不是link_to

标签: ruby-on-rails ruby get put


【解决方案1】:

link_to签名:

link_to(name = nil, options = nil, html_options = nil, &block)

根据文档,:method 属于选项哈希。

打电话

<%= link_to like_post_path(@post), :method => :put do %>

结果投入

  • 名称:like_post_path(@post),
  • 选项:无,
  • html_options: { 方法: :put }

来自Hash

散列通常也被用作在函数中具有命名参数的一种方式。请注意,下面没有使用括号。如果哈希是方法调用的最后一个参数,则不需要大括号,从而创建一个非常干净的接口:

编辑

<%= link_to like_post_path(@post), { method: :put } do %>

导致你想要达到的目标。

【讨论】:

  • 你确定吗? options 不会是 { :method =&gt; :put }html_optionsnil 吗?
  • 我的错误,用正确的解决方案编辑了答案。 (options = { method: :put } 是需要的。)
【解决方案2】:

您使用的是正确的格式,您的代码应该生成如下内容:

<a data-method="put" href="...">

从您的路由错误消息中,我们可以得出结论,它不是使用POST_method=put 参数发送的。所以,问题一定是你没有包含 jQuery 和 Rails 的 jQuery 扩展 javascript 文件。

一个简单的解决方法是在您的页面中包含 application.js 文件(默认包含 jquery 和 rails js 扩展名)。

【讨论】:

  • 这是一个很好的建议,确保设置了 data-method="put",并确保您在 application.js 中请求 jquery_ujs。跨度>
  • 就是这样!!太感谢了。由于stackoverflow.com/questions/28421547/…,问题是我的 Jquery 和 Rails 查询
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 2020-08-04
  • 2011-03-16
  • 1970-01-01
  • 2020-08-01
  • 2019-06-03
  • 2013-06-07
  • 1970-01-01
相关资源
最近更新 更多