【问题标题】:Unable to add new routes using guides无法使用指南添加新路线
【发布时间】:2019-06-18 20:51:26
【问题描述】:

我以前做过,但是在向我的 rails 服务器添加新页面和新路径时遇到了问题。

差不多,我想添加一个新页面,然后在我的网站中链接到该页面...但是我无法让路由创建步骤生效并在执行“rails routes”命令时显示.

我之前在 pages#offerings 和“public_speaking”的“offerings”页面上做过,但我无法让 ruby​​ 使用相同的步骤创建第三个页面,或者看起来是这样。

我开始进入页面控制器并添加“def public_speaking”和“end”:

页面控制器

  def home
  end

  def about
  end

  def offerings
  end

  def public_speaking
  end

  def nonverbal
  end   

end 

Routes.rb

然后在 Routes.rb 中我尝试使用相同的过程(添加 get 'public_speaking', to : 'pages#public_speaking')

  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 'public_speaking', to: 'pages#public_speaking'

  get 'pages/nonverbal'

  get 'nonverbal', to: 'pages#nonverbal'
end 

我尝试了 get 'page', to: 'pages#page' 和 get 'pages/page' 方法来添加路由,但都没有成功。

查看文件

我还在views文件夹中创建了一个同名文件“nonverbal.erb”。

当我做 rails 路线时显示什么

当我运行“rails routes”时:

ubuntu@ip-172-31-91-225:~/environment/saasapp$ 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

我确实有 2 条产品路线,这是否表明有任何问题?

我在创建这条新路径时做错了什么/错过了什么?有什么命令可以执行这个链接吗?

我预计会创建一条新路线(因为它适用于“产品”),但它没有奏效,我不知道为什么。我将重复这个过程 5-6 页,所以我想确保我能做对

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我不知道为什么在您的路线中以及从何处生成offerings GET /offerings(.:format) pages#offerings。您的 routes.rb 似乎由于某种原因不对应。我希望看到的nonverbal 路线在哪里。还有 /nonverbal 和 /pages/nonverbal 等是故意的吗?

    但是我建议尝试更通用的东西,因为您想使用 PagesController.rb 来呈现各种页面。像这样的东西怎么样

    get "pages/:page", to: "pages#serve"
    

    在 PagesController.rb 中

    def serve
       render "pages/#{params[:page]}"
    end
    

    如果您愿意,可以将Constraints 添加到路由

    【讨论】:

      猜你喜欢
      • 2012-10-16
      • 2014-09-20
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多