如何在Rails上创建新资源

时间:2013-12-24 19:06:28

标签: ruby-on-rails routing

我在Windows 7上使用Ruby 1.9.3和Rails(rubygems-1.8.24)。

我成功创建了一个名为'blog'的应用程序。到目前为止,我可以 运行本地服务器(WEBrick)并制作Hello,Rails!出现在 本地主机:3000

但是现在我尝试创建一个新的资源,但无济于事。这是 routes.rb中的代码

Blog::Application.routes.draw do

  resources :posts

  root to: "welcome#index"
end

当我输入:

$ rake routes

我收到此错误消息:

rake aborted!
c:/blog/config/routes.rb: syntax error, unexpected_keyword_end, expecting $end
c:65535:in 'execute_if_updated'
c:/blog/config/environment.rb:5:bin '<top (required)>'
Tasks: TOP => routes => environment
(see full trace by running task with --trace)

何时应显示以下内容:

$ rake routes
    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new
edit_post GET    /posts/:id/edit(.:format) posts#edit
     post GET    /posts/:id(.:format)      posts#show
          PATCH  /posts/:id(.:format)      posts#update
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy
     root        /                         welcome#index

在运行$ rake routes -trace后,我得到:

c:\blog>rake routes --trace
** Invoke routes (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
c:/blog/config/routes.rb:69: syntax error, unexpected keyword_end, expecting $end
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `block in load'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:40:in `each'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:40:in `load_paths'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:16:in `reload!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:26:in `block in updater'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/file_update_checker.rb:75:in `call'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/file_update_checker.rb:75:in `execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application/routes_reloader.rb:27:in `updater'
C:65535:in `execute_if_updated'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `instance_exec'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `run'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
C:/Ruby193/lib/ruby/1.9.1/tsort.rb:150:in `block in tsort_each'
C:/Ruby193/lib/ruby/1.9.1/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
C:/Ruby193/lib/ruby/1.9.1/tsort.rb:219:in `each_strongly_connected_component_from'
C:/Ruby193/lib/ruby/1.9.1/tsort.rb:182:in `block in each_strongly_connected_component'
C:/Ruby193/lib/ruby/1.9.1/tsort.rb:180:in `each'
C:/Ruby193/lib/ruby/1.9.1/tsort.rb:180:in `each_strongly_connected_component'
C:/Ruby193/lib/ruby/1.9.1/tsort.rb:148:in `tsort_each'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/initializable.rb:54:in `run_initializers'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application.rb:215:in `initialize!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
c:/blog/config/environment.rb:5:in `<top (required)>'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `block in require'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application.rb:189:in `require_environment!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/application.rb:250:in `block in run_tasks_blocks'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:236:in `call'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:236:in `block in execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:231:in `each'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:231:in `execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
C:/Ruby193/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_with_call_chain'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:197:in `block in invoke_prerequisites'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:195:in `each'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:195:in `invoke_prerequisites'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:174:in `block in invoke_with_call_chain'
C:/Ruby193/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_with_call_chain'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/task.rb:161:in `invoke'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/application.rb:149:in `invoke_task'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/application.rb:106:in `each'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/application.rb:106:in `block in top_level'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/application.rb:115:in `run_with_threads'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/application.rb:100:in `top_level'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/application.rb:78:in `block in run'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/application.rb:165:in `standard_exception_handling'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/lib/rake/application.rb:75:in `run'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rake-10.1.1/bin/rake:33:in `<top (required)>'
C:/Ruby193/bin/rake:23:in `load'
C:/Ruby193/bin/rake:23:in `<main>'
Tasks: TOP => routes => environment

4 个答案:

答案 0 :(得分:2)

您在--trace输出中收到的错误是:

c:/blog/config/routes.rb:69: syntax error, unexpected keyword_end, expecting $end

这表示错误发生在config/routes.rb文件的第69行。您发布的路线文件内容只有6行,所以我猜您没有发布整个routes.rb。如果您发布整个文件,那么有人可能会帮助您,但原样,我们不能。

答案 1 :(得分:1)

您的路线文件夹中的第一行可能不正确。您有routes小写字母。尝试使用以下代码替换routes.rb文件的第一行:

Blog::Application.Routes.draw do

答案 2 :(得分:0)

将您的路线文件更改为:

root "welcome#index"

答案 3 :(得分:0)

这就是你的config / routes.rb应该是什么样子

Rails.application.routes.draw do
  # get 'welcome/index'

  # 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#index'

  # 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

Rails.application.routes.draw do

  resources :articles

  root 'welcome#index'
end