Ruby-on-rails Rails 中的匹配和路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18368933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:34:16  来源:igfitidea点击:

Matching and Routes in Rails

ruby-on-railsrubyroutes

提问by megashigger

I generated a controller and changed the routes but opening the links yields errors on my local server.

我生成了一个控制器并更改了路由,但打开链接会在我的本地服务器上产生错误。

Generating controller and routes

生成控制器和路由

rails generate controller StaticPages home about team contact

Change routes.rb

更改路线.rb

MyApp::Application.routes.draw do
  root to: 'static_pages#home'

  match '/about',    to: 'static_pages#about'
  match '/team',     to: 'static_pages#team'
  match '/contact',  to: 'static_pages#contact'
end

The root path work but none of the 'about, 'team', or 'contact' links work. This is the error I get:

根路径有效,但“关于”、“团队”或“联系人”链接均无效。这是我得到的错误:

"You should not use the matchmethod in your router without specifying an HTTP method. If you want to expose your action to both GET and POST, add via: [:get, :post]option. If you want to expose your action to GET, use getin the router: Instead of: match "controller#action" Do: get "controller#action""

“您不应该match在没有指定 HTTP 方法的情况下在路由器中使用该方法。如果您想将您的操作公开给 GET 和 POST,请添加via: [:get, :post]选项。如果您想将您的操作公开给 GET,请get在路由器中使用:而不是:匹配 "controller#action" 做:获取 "controller#action""

Why can't I use 'match'?

为什么我不能使用“匹配”?

回答by Jason Kim

matchmethod has been deprecated.

match方法已被弃用。

Use getfor GET and postfor POST.

使用get了GET和post为POST。

get '/about', to: 'static_pages#about'

get '/about', to: 'static_pages#about'

回答by Jason Kim

You can use match, you've gotta add a via:option:

你可以使用match,你必须添加一个via:选项:

match '/about',    to: 'static_pages#about', via: :get
match '/team',     to: 'static_pages#team', via: :get
match '/contact',  to: 'static_pages#contact', via: :get

You can also pass other HTTP verbs to via:if you need to, like via: [:get, :post]

via:如果需要,您还可以将其他 HTTP 动词传递给,例如via: [:get, :post]

Source: Rails Routing Guide

来源:Rails 路由指南

回答by duykhoa

First, you must specify the HTTP method by adding via: :getat the end of match 'st' => 'controller#action

首先,您必须通过via: :get在末尾添加来指定 HTTP 方法match 'st' => 'controller#action

And it's better to use get '/home', to: 'static_pages#home'

而且最好用 get '/home', to: 'static_pages#home'

But, there is a problem, that your code doesn't follow RESTful, that only support 7 actions: index, new, edit, create, update, show and destroy.

但是,有一个问题,您的代码不遵循 RESTful,仅支持 7 个操作:索引、新建、编辑、创建、更新、显示和销毁。

These are 2 solutions:

这是2个解决方案:

SOL 1: Put them in different controller (homes, abouts..) and all of these controllers have action index.

SOL 1:将它们放在不同的控制器(homes、abouts..)中,所有这些控制器都有 action index

SOL 2: If it's too much work, we can match them to showaction. We use static_pages controller, and each page (home, about) will be a item.

SOL 2:如果工作量太大,我们可以将它们与show行动相匹配。我们使用 static_pages 控制器,每个页面(主页,关于)将是一个项目。

The routes will look likes /static_pages/home /static_pages/about

路线看起来像 /static_pages/home /static_pages/about

I know it isn't good because of the prefix static_pages.

我知道这不好,因为前缀static_pages.

We can easily get rid of this by adding a custom routes at the end of routes file:

我们可以通过在路由文件的末尾添加自定义路由来轻松摆脱这一点:

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

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

That's it. And if you think it's too much work (I think so too), check out this gem High Voltage. Have fun.

就是这样。如果您认为工作量太大(我也这么认为),请查看这个 gem High Voltage。玩得开心。