我在rails上打开localhost ruby时尝试将自定义主页设置为默认主页。我在这里和本教程http://blog.teamtreehouse.com/static-pages-ruby-rails上关注了两个问题的答案。我也试图在notepad ++中编写我的代码,以便它可以在我的网络浏览器(谷歌浏览器)上显示。如果有人可以帮助我完成教程我试图关注团队树屋或给我另一个答案,我会非常感激。谢谢
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'Welcome#code'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
答案 0 :(得分:1)
您应该执行以下操作:
#config/routes.rb
root "welcome#code"
#app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
def code
end
end
#app/views/welcome/code.html.erb
Hello world!
这样您就可以浏览到localhost:3000
(lvh.me:3000
)并显示welcome#code
的内容。
作为一个扩展(因为你是初学者),任何&#34;随机&#34;控制器中的方法应放入ApplicationController
。这样,您就不必添加不必要的WelcomeController
...
#config/routes.rb
root "application#welcome"
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def welcome
end
end
#app/views/application/welcome.html.erb
Hello world.
答案 1 :(得分:0)
在有机会帮助别人的时候,我已经设置了运行配置以将我定向到另一个页面。愚蠢的错误,但也许会帮助别人。
在RubyMine中:
运行->配置->运行浏览器
并且只需确保它可以将您定向到您要访问的网址即可。