Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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如何知道哪个控制器';s";“显示”;在本例中使用Rails指南中的操作?_Ruby On Rails_Ruby_Ruby On Rails 3_Routes_Actioncontroller - Fatal编程技术网

Ruby on rails Rails如何知道哪个控制器';s";“显示”;在本例中使用Rails指南中的操作?

Ruby on rails Rails如何知道哪个控制器';s";“显示”;在本例中使用Rails指南中的操作?,ruby-on-rails,ruby,ruby-on-rails-3,routes,actioncontroller,Ruby On Rails,Ruby,Ruby On Rails 3,Routes,Actioncontroller,我正在回顾Rails指南的部分,我遇到了以下部分: 有时,您有一个资源,客户机总是在没有它的情况下查找它 引用ID。例如,您希望/profile始终显示 当前登录用户的配置文件。在这种情况下,您可以使用 将/profile(而不是/profile/:id)映射到 采取行动: get 'profile', to: 'users#show' 传递要匹配的字符串将需要控制器#动作格式, 传递符号时,符号将直接映射到动作: get 'profile', to: :show 指南说“传递符号将直接映射

我正在回顾Rails指南的部分,我遇到了以下部分:

有时,您有一个资源,客户机总是在没有它的情况下查找它 引用ID。例如,您希望/profile始终显示 当前登录用户的配置文件。在这种情况下,您可以使用 将/profile(而不是/profile/:id)映射到 采取行动:

get 'profile', to: 'users#show'
传递要匹配的字符串将需要控制器#动作格式, 传递符号时,符号将直接映射到动作:

get 'profile', to: :show

指南说“传递符号将直接映射到动作”,但假设我有多个控制器,每个控制器都有一个“显示”动作。既然我不再引用特定的控制器,Rails如何知道使用哪个控制器?

它似乎不知道

#config/routes.rb

get 'profile', to: :show
从控制台:

$ rake routes
rake aborted
missing :controller
第一个例子:“用户#show”确实创建了一条成功的路线,正如它一样

get 'profile', to: :show, controller: 'users'

看来它不知道

#config/routes.rb

get 'profile', to: :show
从控制台:

$ rake routes
rake aborted
missing :controller
第一个例子:“用户#show”确实创建了一条成功的路线,正如它一样

get 'profile', to: :show, controller: 'users'

那份文件有点误导人,不是吗。这是一个糟糕的例子。关于传递要匹配的符号的注释是一个一般性注释,应该在关于单一资源的部分之外

事实上,如果您单独尝试,在启动Rails或运行
rake routes
时会出现以下错误:

rake routes
rake aborted!
missing :controller
因此,您必须添加一个
:controller
选项,使其在资源声明之外工作:

get 'profile', to: :show, controller: 'users'
它们指定的语法在
资源
资源
声明中有效,例如:

resources :user do
  collection do
    get 'profile', to: :show
  end
end


但是,这两个示例生成的路由与前面的示例不同。因此,我再次认为该评论是错误的。

该文档有点误导,不是吗。这是一个糟糕的例子。关于传递要匹配的符号的注释是一个一般性注释,应该在关于单一资源的部分之外

事实上,如果您单独尝试,在启动Rails或运行
rake routes
时会出现以下错误:

rake routes
rake aborted!
missing :controller
因此,您必须添加一个
:controller
选项,使其在资源声明之外工作:

get 'profile', to: :show, controller: 'users'
它们指定的语法在
资源
资源
声明中有效,例如:

resources :user do
  collection do
    get 'profile', to: :show
  end
end


但是,这两个示例生成的路由与前面的示例不同。因此,我再次认为注释放错了位置。

似乎行
get'profile',to:'users#show',controller:'users'
应该是
get'profile',to::show,controller:'users'
。如果我们以第一种方式写入行,
controller
选项无效。似乎行
get'profile',to:'users#show',controller:'users'
应该是
get'profile',to::show,controller:'users'
。如果以第一种方式写入行,
controller
选项无效。