【问题标题】:Routing Error No route matches [GET]路由错误没有路由匹配[GET]
【发布时间】:2017-03-15 18:45:12
【问题描述】:

我做了一个控制器,得到以下错误信息

路由错误没有路由匹配 [GET] "handm/index"

(这些是错误表的标题)

Routes 路由从上到下匹配优先级 Helper HTTP Verb 路径控制器#Action 路径/Url

(这是内容)handm_index_path GET /handm/index(.:format) handm#index root_path GET / handm#index 请求参数:

请有人解释一下这个错误的含义。以下是我的路线文件。

Rails.application.routes.draw do
  get '/handm/index' 
  root :to => "handm#index"
end

【问题讨论】:

  • 尝试使用get '/handm/index' => "handm#index" 而不是get '/handm/index'
  • 这不起作用。
  • 你能给你控制器文件,连同它的名字
  • class HandmController < ApplicationController def index end end
  • 能否请您解释一下错误消息的一般格式?识别消息中的实际错误的有用信息是什么。目前对我来说似乎是胡言乱语。谢谢

标签: ruby-on-rails


【解决方案1】:
Rails.application.routes.draw do      
  get '/handm/index', to: 'handm#index'
  root :to => "handm#index"
end

【讨论】:

  • 这也不起作用。同样的错误信息。谢谢回复
【解决方案2】:

我已经解决了,

你不需要输入http://127.0.0.1:3000/index

就这个http://127.0.0.1:3000/ 因为您告诉 rails 让您以 root 身份进行索引。

但是你会遇到关于 javascript 的问题, 要解决你需要编辑你的 application.js

编辑这一行 //= require_tree 。 to = require_tree 。

希望这会有所帮助

【讨论】:

    【解决方案3】:

    试试下面的代码:

    routes.rb

    Rails.application.routes.draw do
      get '/handm/index' => 'handm#index'
      root :to => "handm#index"
    end
    

    handm_controller.rb

    class HandmController < ApplicationController 
      def index 
        ...
      end 
    end
    

    views/handm/index.html.erb

    <h1>In index page</h1>
    

    通过命令启动你的服务器:

    rails server
    

    点击网址:

    localhost:3000
    

    localhost:3000/handm/index
    

    【讨论】:

      【解决方案4】:

      错误表示指定的路由或路径不存在。因此,解决方案是将其更改为有效路径。我们只能猜测您的 Rails 应用程序支持的路径,但 /handm#index 是合理的:

      Rails.application.routes.draw do
        get '/handm/index' => 'handm#index'
        root :to => "handm#index"
      end
      

      【讨论】:

      • 我认为我没有投反对票。如果我这样做是错误的
      猜你喜欢
      • 2014-03-26
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2013-07-19
      相关资源
      最近更新 更多