首页 > 解决方案 > 创建路线在一个分支中有效,但在另一个分支中无效

问题描述

我通过在 pages_controller.rb 中添加一个条目创建了一个新的“public_speaking”路由:

def public_speaking
end

我还为“public_speaking.html.erb”创建了一个准系统视图文件,并在 routes.rb 中添加了一个路由,如下所示:

get 'home/public_speaking'

-或者-

get 'public_speaking', to: 'pages#public_speaking'

我已经尝试了该路线的两个版本,并且它在我的一个分支中工作,但是我尝试将代码复制并粘贴到另一个分支中,并且当我运行“rails 路线”时,相同的步骤不会为“public_speaking”路线产生任何条目“在我的控制台中。

到底是什么导致在一个分支而不是另一个分支中创建新路线?我还应该尝试什么?

完整的 routes.rb 文件如下所示:

Rails.application.routes.draw do
  root to: "pages#home"

  get 'home/public_speaking'

  get 'public_speaking', to: 'pages#public_speaking'

  devise_for :users, controllers: { registrations: 'users/registrations' }
  resources :users do 
      resource :profile
  end 

  get 'about', to: 'pages#about'

  resources :contacts, only: [:create]

  get 'contact-us', to: 'contacts#new', as: 'new_contact'

  get 'home/public_speaking'

  get 'public_speaking', to: 'pages#public_speaking'

end 

完整页面控制器如下所示:

class PagesController < ApplicationController
  # GET request for / which is our home page
      def home 
        @basic_plan = Plan.find(1)
        @pro_plan = Plan.find(2)
      end 

      def about
      end 

      def offerings
      end 

      def public_speaking
      end
end 

编辑为工作分支添加“rails routes”的结果:

rails routes
                  Prefix Verb   URI Pattern                            Controller#Action
              pages_home GET    /pages/home(.:format)                  pages#home
             pages_about GET    /pages/about(.:format)                 pages#about
         pages_offerings GET    /pages/offerings(.:format)             pages#offerings
   pages_public_speaking GET    /pages/public_speaking(.:format)       pages#public_speaking
                    root GET    /                                      pages#home
        new_user_session GET    /users/sign_in(.:format)               devise/sessions#new
            user_session POST   /users/sign_in(.:format)               devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)              devise/sessions#destroy
           user_password POST   /users/password(.:format)              devise/passwords#create
       new_user_password GET    /users/password/new(.:format)          devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)         devise/passwords#edit
                         PATCH  /users/password(.:format)              devise/passwords#update
                         PUT    /users/password(.:format)              devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                users/registrations#cancel
       user_registration POST   /users(.:format)                       users/registrations#create
   new_user_registration GET    /users/sign_up(.:format)               users/registrations#new
  edit_user_registration GET    /users/edit(.:format)                  users/registrations#edit
                         PATCH  /users(.:format)                       users/registrations#update
                         PUT    /users(.:format)                       users/registrations#update
                         DELETE /users(.:format)                       users/registrations#destroy
            user_profile POST   /users/:user_id/profile(.:format)      profiles#create
        new_user_profile GET    /users/:user_id/profile/new(.:format)  profiles#new
       edit_user_profile GET    /users/:user_id/profile/edit(.:format) profiles#edit
                         GET    /users/:user_id/profile(.:format)      profiles#show
                         PATCH  /users/:user_id/profile(.:format)      profiles#update
                         PUT    /users/:user_id/profile(.:format)      profiles#update
                         DELETE /users/:user_id/profile(.:format)      profiles#destroy
                   users GET    /users(.:format)                       users#index
                         POST   /users(.:format)                       users#create
                new_user GET    /users/new(.:format)                   users#new
               edit_user GET    /users/:id/edit(.:format)              users#edit
                    user GET    /users/:id(.:format)                   users#show
                         PATCH  /users/:id(.:format)                   users#update
                         PUT    /users/:id(.:format)                   users#update
                         DELETE /users/:id(.:format)                   users#destroy
                   about GET    /about(.:format)                       pages#about
                contacts POST   /contacts(.:format)                    contacts#create
             new_contact GET    /contact-us(.:format)                  contacts#new
               offerings GET    /offerings(.:format)                   pages#offerings
ubuntu@ip-172-31-91-225:~/environment/saasapp$ 

以及它不起作用的分支:

 rails routes
                  Prefix Verb   URI Pattern                            Controller#Action
                    root GET    /                                      pages#home
        new_user_session GET    /users/sign_in(.:format)               devise/sessions#new
            user_session POST   /users/sign_in(.:format)               devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)              devise/sessions#destroy
           user_password POST   /users/password(.:format)              devise/passwords#create
       new_user_password GET    /users/password/new(.:format)          devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)         devise/passwords#edit
                         PATCH  /users/password(.:format)              devise/passwords#update
                         PUT    /users/password(.:format)              devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                users/registrations#cancel
       user_registration POST   /users(.:format)                       users/registrations#create
   new_user_registration GET    /users/sign_up(.:format)               users/registrations#new
  edit_user_registration GET    /users/edit(.:format)                  users/registrations#edit
                         PATCH  /users(.:format)                       users/registrations#update
                         PUT    /users(.:format)                       users/registrations#update
                         DELETE /users(.:format)                       users/registrations#destroy
            user_profile POST   /users/:user_id/profile(.:format)      profiles#create
        new_user_profile GET    /users/:user_id/profile/new(.:format)  profiles#new
       edit_user_profile GET    /users/:user_id/profile/edit(.:format) profiles#edit
                         GET    /users/:user_id/profile(.:format)      profiles#show
                         PATCH  /users/:user_id/profile(.:format)      profiles#update
                         PUT    /users/:user_id/profile(.:format)      profiles#update
                         DELETE /users/:user_id/profile(.:format)      profiles#destroy
                   users GET    /users(.:format)                       users#index
                         POST   /users(.:format)                       users#create
                new_user GET    /users/new(.:format)                   users#new
               edit_user GET    /users/:id/edit(.:format)              users#edit
                    user GET    /users/:id(.:format)                   users#show
                         PATCH  /users/:id(.:format)                   users#update
                         PUT    /users/:id(.:format)                   users#update
                         DELETE /users/:id(.:format)                   users#destroy
                   about GET    /about(.:format)                       pages#about
                contacts POST   /contacts(.:format)                    contacts#create
             new_contact GET    /contact-us(.:format)                  contacts#new
               offerings GET    /offerings(.:format)                   pages#offerings

标签: ruby-on-rails

解决方案


推荐阅读