【问题标题】:<Ruby on Rails> Routes<Ruby on Rails> 路由
【发布时间】:2013-08-19 06:08:38
【问题描述】:

我是 ruby​​ on rails 的初学者,正在学习在线教程。我立刻迷路了。每次我尝试访问我的 url 'pages/help' 时,浏览器中都会显示一个错误“找不到 PagesController 的操作 'show'”。我有一个名为 _header 的 ruby​​ 分区,其中包含以下代码:

<header>
    <%= link_to logo, root_path %>


    <ul class="nav nav-tabs">
      <li class="active">
        <%= link_to "Home |", root_path %>
      </li>

      <li><%= link_to "Help |", pages_help_path %></li>
      <li><%= link_to "Sign In", '#'%></li>
    </ul>
</header>

当我尝试点击“帮助”链接时,就会出现错误。

我的 routes.rb 只包含这些代码:

BakeShop::Application.routes.draw do

  resources :pages
  root :to => 'pages#home'

  #match '/help', :to => 'pages#help'
  get "pages/help"
end

我有一个只有这些代码的 PagesController:

class PagesController < ApplicationController
  def home

  end

  def help

  end
end

当我运行“rake routes”时,包含的列表是:

     pages GET    /pages(.:format)          pages#index
           POST   /pages(.:format)          pages#create
  new_page GET    /pages/new(.:format)      pages#new
 edit_page GET    /pages/:id/edit(.:format) pages#edit
      page GET    /pages/:id(.:format)      pages#show
           PATCH  /pages/:id(.:format)      pages#update
           PUT    /pages/:id(.:format)      pages#update
           DELETE /pages/:id(.:format)      pages#destroy
      root GET    /                         pages#home
pages_help GET    /pages/help(.:format)     pages#help

我当然在我的视图文件夹中创建了 home.html.erb 和 help.html.erb。扭曲的是,当我在我的视图文件夹中创建一个“show.html.erb”并在我的页面控制器中创建一个名为“show”的操作时,错误消失并链接到 .

所以我的意思是,谁能解释一下?为什么 rails 会寻找我没有定义的“展示”动作而不是“帮助”动作

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    关于 Rails 中的路由,有一点需要理解,即指定的路由顺序非常重要。在您的 config/routes.rb 文件中,您按以下顺序指定了路由:

    resources :pages
    root :to => 'pages#home'
    get "pages/help"
    

    所以,它的作用是首先匹配resources :pages 定义的路由。正如您在rake routes 的输出中看到的那样,Rails 为该特定行生成了以下路由,即resources :pages

         pages GET    /pages(.:format)          pages#index
               POST   /pages(.:format)          pages#create
      new_page GET    /pages/new(.:format)      pages#new
     edit_page GET    /pages/:id/edit(.:format) pages#edit
          page GET    /pages/:id(.:format)      pages#show
               PATCH  /pages/:id(.:format)      pages#update
               PUT    /pages/:id(.:format)      pages#update
               DELETE /pages/:id(.:format)      pages#destroy
    

    另请注意,pages_help GET /pages/help(.:format) pages#help 位于该列表的底部。因此,当您导航到 /pages/help 时,Rails 会找到与该模式匹配的第一个 GET 请求,在这种情况下:

    page GET    /pages/:id(.:format)      pages#show
    

    由于您没有定义 show 操作,它会抛出错误“找不到 PagesController 的操作‘显示’”。

    所以,为了解决您没有show 操作和show.html.erb 的具体问题,您可以重新排列config/routes.rb,如下所示:

    BakeShop::Application.routes.draw do
      get "pages/help" # Move this line before resources :pages
      resources :pages
      root :to => 'pages#home'
    end
    

    移动get "pages/help" 将确保对于您的网址/pages/help,此路由将匹配并且您的help.html.erb 将被呈现。

    更新:

    它如何匹配pages/show

    # In the comments below where .format is used think of various extensions just as html, js, json, xml etc but not required.
    
    pages GET    /pages(.:format)          pages#index
    # Matches /pages, /pages.format
    
    new_page GET    /pages/new(.:format)      pages#new
    # Matches /pages/new, /pages/new.format
    
    edit_page GET    /pages/:id/edit(.:format) pages#edit
    # Matches /pages/1/edit, /pages/1/edit.format or pages/anything/edit.format.  Note that the edit in the end is required here,the only variable here is id and format
    
    page GET    /pages/:id(.:format)      pages#show
    # Matches /pages/1, /pages/anything, /pages/1.format, /pages/anything.format.  In the question /pages/help matches.
    

    【讨论】:

    • 我喜欢它!很好的答案和解释。如果你不介意我再问一个问题,rails 传递的这个 GET 模式是什么?因为当您说“Rails 找到第一个 GET 请求”时,pages_path、new_page_path 和 edit_page_path 不都是 GET 请求吗?为什么 rails 不匹配它们呢?
    • @Mr.Slow,很高兴你喜欢我的回答。我已经添加了一些更新以供您发表评论,如果我不清楚,请查看并发表评论。希望这会有所帮助。
    【解决方案2】:

    您必须在您的 routes.rb 中启用默认根目录。

    更新 routes.rb:-

    BakeShop::Application.routes.draw 做 资源:页面 根 :to => 'pages#home' #match '/help', :to => 'pages#help' 获取“页面/帮助”#这不是必需的 匹配 ':controller(/:action(/:id(.:format)))' 结尾

    【讨论】:

      【解决方案3】:

      这里的问题是show action (page_path) 的路由和pages_help_path 的路由冲突。如果您只是将行 get "pages/help" 移到行 resources :pages 之前,它将按您的意愿工作。

      Routes.rb:

      BakeShop::Application.routes.draw do
        get "pages/help"
        resources :pages
        root :to => 'pages#home'
      end
      

      【讨论】:

        猜你喜欢
        • 2011-04-17
        • 1970-01-01
        • 1970-01-01
        • 2014-11-08
        • 2018-12-28
        • 2011-05-11
        • 2013-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多