【问题标题】:I am defining post method in link_to but i am getting routing error for get , i have defined a post route我在 link_to 中定义了 post 方法,但我得到了 get 路由错误,我已经定义了一个 post 路由
【发布时间】:2019-11-01 20:29:11
【问题描述】:

我在我的 routes.rb 中定义了一个 post 路由,并在 link_to helper 中使用它。当我点击由link_to helper 生成的链接时,我收到一个错误,假设是 get 方法的错误。

Started GET "/wallpapers/1/like?like=true" for 127.0.0.1 at 2017-08-03 12:51:59 +0530

ActionController::RoutingError (No route matches [GET] "/wallpapers/1/like"):

actionpack (5.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.4) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.4) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.4) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.4) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.4) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.4) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.4) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.4) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.4) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.0.4) lib/rails/engine.rb:522:in `call'
puma (3.9.1) lib/puma/configuration.rb:224:in `call'
puma (3.9.1) lib/puma/server.rb:602:in `handle_request'
puma (3.9.1) lib/puma/server.rb:435:in `process_client'
puma (3.9.1) lib/puma/server.rb:299:in `block in run'
puma (3.9.1) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms)
  Rendered collection of C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/routes/_route.html.erb [11 times] (3.5ms)
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/routes/_table.html.erb (5.0ms)
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.5ms)
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.4/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (1391.8ms)

我的 routes.rb 文件

Rails.application.routes.draw do  
  root 'pages#home'
  get '/home', to: 'pages#home'

  resources :wallpapers do
    member do  
      post 'like'
    end
  end
end

我的链接地址

<%= link_to like_wallpaper_path(@wallpaper), :method => :post  do %>
  <i class="glyphicon glyphicon-thumbs-up"></i>
<% end %> 

【问题讨论】:

  • 错误是?
  • 错误是没有路由匹配 [GET] "/wallpapers/2/like"
  • 请提供完整的回溯
  • @ArjunSingh 在您的终端中运行rake routes。它显示带有参数和类型的路由列表。检查请求的路由是否可用。
  • 会不会是您的浏览器禁用了 JavaScript?或者您没有通过简单的鼠标左键单击来使用链接? link_to 关于 :method 选项的文档是:Supported verbs are :post, :delete, :patch, and :put. Note that if the user has JavaScript disabled, the request will fall back to using GET.

标签: ruby-on-rails ruby


【解决方案1】:

而不仅仅是method: :postdata: { method: 'post' }。所以你的链接应该是这样的

<%= link_to like_wallpaper_path(@wallpaper), data: { method: 'post' }  do %>
    <i class="glyphicon glyphicon-thumbs-up"></i>
<% end %>

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 2021-09-07
    • 1970-01-01
    • 2022-12-20
    • 2014-11-03
    • 1970-01-01
    • 2021-12-15
    • 2014-05-22
    • 2023-03-09
    相关资源
    最近更新 更多