简体   繁体   English

为什么会出现路由错误?

[英]why am I getting a routing error?

I am following a RoR tutorial and the routing file that was gererated in the tutorial is different to the one that was generated on my computer. 我正在关注RoR教程,该教程中生成的路由文件与计算机上生成的路由文件不同。 The one on the tutorial was 本教程中的一个是

Lecture::Application.routes.draw.do
  get 'say/hello'

my file gererated 我的文件已损坏

Rails.application.routes.draw.do
  get 'say/hello'

Does this make a difference because I am getting a routing error: 这会有所不同,因为我遇到了路由错误:

Routing Error No route matches [GET] "/say/hello" 路由错误没有路由匹配[GET]“ / say / hello”

What is the reason for this? 这是什么原因呢?

You need to specify where the route leads. 您需要指定路线的通向。 For example: ( Rails Routing from the Outside In ) 例如:( 从外部进入Rails路由

get '/patients/:id', to: 'patients#show'

or in your case: 或者您的情况:

get 'say/hello', to: 'say#hello'

The to formatting is 'controller_name#controller_action' 格式为“ controller_name#controller_action”

To define an action use: 要定义动作,请使用:

def hello
  #This is my hello action
end

If you have the say controller and hello method in that controller, 如果您在该控制器中具有say控制器和hello方法,

change, 更改,

get 'say/hello', to: 'say#hello'

to, 至,

get '/say/hello', to: 'say#hello'

Observe, '/say/hello' . 观察'/say/hello'

If you dont have that controller, 如果您没有那个控制器,

type rake routes in rails console and update your question. rails console输入rake routes并更新您的问题。

#config/routes.rb

get 'say/hello' => 'say#hello' #Here say is controller and hello is action

#controllers/say_controller.rb

class SayController < ApplicationController
  def hello
    ...
  end
end

your controller file should be: 您的控制器文件应为:

say_controller.rb

Class SayController < ApplicationController
  def hello
   puts 'hello there'
  end
end

your routes.rb file should contain: 您的routes.rb文件应包含:

routes.rb

get '/anything/you_want/', to 'say#hello'

this should do the trick 这应该可以解决问题

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

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