我正在阅读本教程:http ://edgeguides.rubyonrails.org/getting_started.html 我要提到的一件事是,我认为我使用的 Rails 版本与本教程不同。我正在使用版本 2.3.14 我正在使用 Linux 系统。我因此建立了一个rails项目:
rails myproject
正如教程所说,它设置了许多目录和文件(尽管教程使用的命令略有不同)
接下来,我通过创建一个 COntroller 和 View 来设置我的第一个应用程序,如下所示:
ruby script/generate controller welcome index
这在我的项目的 app 目录中,在控制器和视图子目录中创建了文件。也就是说,我知道在视图目录中有文件 index.html.erb。
接下来我只想测试服务器:
ruby script/server
然后在我的浏览器中转到 localhost:3000,这将我带到公共目录中的 index.html 文件。
这就是事情出错的地方。我想被定向到视图子目录中的 index.html.erb 文件。所以,我去配置并打开 routes.rb 文件进行编辑。以下是我编辑的文件:
ActionController::Routing::Routes.draw do |map|
# The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products
# Sample resource route with options:
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
# Sample resource route with sub-resources:
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
# Sample resource route with more complex sub-resources
# map.resources :products do |products|
# products.resources :comments
# products.resources :sales, :collection => { :recent => :get }
# end
# Sample resource route within a namespace:
# map.namespace :admin do |admin|
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
# admin.resources :products
# end
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
map.root :controller => "welcome#index"#I CHANGED THIS LINE
# See how all your routes lay out with "rake routes"
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing or commenting them out if you're using named routes and resources.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
我只对文件进行了一次更改。我所做的只是取消注释该行
map.root :controller => "welcome#index"
并将它指向我的 index.html.erb 文件(至少我认为我正在这样做)。重新启动服务器仍然会将我发送到 index.html(在公共目录中)。所以我删除了文件。杀死服务器,重新启动。现在我收到一条错误消息。我无法读取我的 index.html.erb 文件。我需要任何帮助来解决这个问题。