我正在阅读本教程:http://edgeguides.rubyonrails.org/getting_started.html 我要提到的一件事是我认为我在教程中使用了不同版本的Rails。我使用的是2.3.14版 我正在研究Linux系统。我设置了一个rails项目:
rails myproject
正如教程所说,设置了许多目录和文件(尽管教程使用的命令略有不同)
接下来,我通过创建一个COntroller和View来设置我的第一个应用程序,如下所示:
ruby script/generate controller welcome index
这在我的项目的app目录中,在controllers和views子目录中创建了文件。也就是说,我知道在views目录中有文件index.html.erb。
接下来我只想测试服务器:
ruby script/server
然后在我的浏览器中转到localhost:3000,它将我带到公共目录中的index.html文件。
这是出问题的地方。我希望被定向到views子目录中的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文件。我想帮助解决这个问题。
答案 0 :(得分:2)
rails 2.x和rails 3.x存在一些主要差异。如果你正在关注edgerails教程,那就是3.x. Install the new version of rails。然后,您应该按照教程中的示例进行操作。
编辑:事实上,安装说明在教程中。如果您在安装ruby 1.9时遇到问题,我更喜欢rbenv来管理ruby版本。 RVM可能是更受欢迎的工具。
答案 1 :(得分:2)
试用此版本的Rails指南:http://guides.rubyonrails.org/v2.3.11/
但是,我强烈建议使用更新版本的Rails(目前为3.2)开始学习。这可能会为您节省一些时间和精力。