Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 5.1:Globbing不';不行?_Ruby On Rails_Routing - Fatal编程技术网

Ruby on rails Rails 5.1:Globbing不';不行?

Ruby on rails Rails 5.1:Globbing不';不行?,ruby-on-rails,routing,Ruby On Rails,Routing,routes.rb /posts/1重定向到posts\show,但/posts/asdf不会重定向到posts\missing 全球定位在这里不起作用。为什么? 我正在使用Rails 5.1,在5.0上看到了一个配置完全相同的教程,globbing捕获了无效ID。 我知道我可以从中使用rescue\u,但我想通过routes.rb实现这一点,我将如何实现 我使用的是Rails 5.1,在5.0上看到了一个配置完全相同的教程,globbing捕捉到了无效的ID 我认为这在早期版本的rails(当

routes.rb

/posts/1重定向到
posts\show
,但/posts/asdf不会重定向到
posts\missing

全球定位在这里不起作用。为什么? 我正在使用Rails 5.1,在5.0上看到了一个配置完全相同的教程,globbing捕获了无效ID。
我知道我可以从中使用
rescue\u,但我想通过routes.rb实现这一点,我将如何实现

我使用的是Rails 5.1,在5.0上看到了一个配置完全相同的教程,globbing捕捉到了无效的ID

我认为这在早期版本的rails(当然不是5.0)上不起作用。来自post资源的
show
路由匹配
/posts/asdf
路径,并尝试使用id的
“asdf”
呈现
posts\show
。这是很长一段时间以来的情况。

资源:posts,除了::show之外
获取“posts/*missing/”,至:“posts#missing”

EDT-刚刚测试过,意识到这可能会覆盖一些有效的路由-不确定OP问题的最终答案

路由文件中方法的顺序似乎在这里起作用。 我在使用(inside routes.rb文件)时遇到了相同的错误:

改为

  get 'posts/*missing', to: 'posts#missing'
  resources :posts

它可以工作:)

你是错过了路线中的单引号还是打字错误?给我们看看那个教程。你是对的!将
get
路由置于
resources:posts
之上应该可以修复错误。不是吗?哦!我认为通过颠倒顺序,错误将得到修复,因为Rails开始从顶部匹配路由。您好,欢迎来到stack overflow。您可以使用此链接改进您的答案并
  resources :posts
  get 'posts/*missing', to: 'posts#missing'
  get 'posts/*missing', to: 'posts#missing'
  resources :posts