ror:路由配置/查看/使用

由于本人使用的是Rails 5.0.1,但是参考的学习资料室《Ruby on Rails 3th》,版本有差异,导致有些测试代码提示错误,在这里仅以两个例子展示:

问题

在写测试post :create, micropost: { content: "Lorem ipsum" }delete :destroy, id: @micropost时提示错误。
---- 《Ruby on Rails 3th》11.3.1节

分析

在所有代码没有问题的情况下,只有这两处测试代码有问题,可以肯定是测试代码的问题。可以查看一下错误提示:

... ...
URI::InvalidURIError:         URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80create
... ...
URI::InvalidURIError:         URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80destroy
... ...

可以看到是URL的问题,但是具体有何问题呢?我们在获取注册地址时,使用的是get signup_url,那类似的猜测这里也应该是***_url。我们查看一下路由。此时我的路由配置为:

Rails.application.routes.draw do
  get 'password_resets/new'
  get 'password_resets/edit'

  get 'login' => 'sessions#new'
  post 'login' => 'sessions#create'
  delete 'logout' => 'sessions#destroy'

  get 'help' => 'static_pages#help'
  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup' => 'users#new'
  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets, only: [:new, :create, :edit, :update]
  resources :microposts, only: [:create, :destroy]

  root "static_pages#home"
end

查看路由

我们要查看具体的路由路径,但是不知道用什么命令。此时可以使用help命令查看:

➜  microblog git:(user-micropost) ✗ rails --help
Usage: rails COMMAND [ARGS]

The most common rails commands are:
 generate    Generate new code (short-cut alias: "g")
 console     Start the Rails console (short-cut alias: "c")
 server      Start the Rails server (short-cut alias: "s")
 test        Run tests (short-cut alias: "t")
 dbconsole   Start a console for the database specified in config/database.yml
             (short-cut alias: "db")
 new         Create a new Rails application. "rails new my_app" creates a
             new application called MyApp in "./my_app"

All commands can be run with -h (or --help) for more information.

In addition to those commands, there are:
... ...
 routes                              Print out all defined routes in match order, with names
... ...

可以看到有routes命令(输出所有定义的路由),执行rails routes命令:

➜  microblog git:(user-micropost) ✗ rails routes
                 Prefix Verb   URI Pattern                             Controller#Action
    password_resets_new GET    /password_resets/new(.:format)          password_resets#new
   password_resets_edit GET    /password_resets/edit(.:format)         password_resets#edit
                  login GET    /login(.:format)                        sessions#new
                        POST   /login(.:format)                        sessions#create
                 logout DELETE /logout(.:format)                       sessions#destroy
                   help GET    /help(.:format)                         static_pages#help
                  about GET    /about(.:format)                        static_pages#about
                contact GET    /contact(.:format)                      static_pages#contact
                 signup GET    /signup(.:format)                       users#new
                  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
edit_account_activation GET    /account_activations/:id/edit(.:format) account_activations#edit
        password_resets POST   /password_resets(.:format)              password_resets#create
     new_password_reset GET    /password_resets/new(.:format)          password_resets#new
    edit_password_reset GET    /password_resets/:id/edit(.:format)     password_resets#edit
         password_reset PATCH  /password_resets/:id(.:format)          password_resets#update
                        PUT    /password_resets/:id(.:format)          password_resets#update
             microposts POST   /microposts(.:format)                   microposts#create
              micropost DELETE /microposts/:id(.:format)               microposts#destroy
                   root GET    /                                       static_pages#home

在这些路径中,可以看到:

microposts  POST   /microposts(.:format)     microposts#create
micropost   DELETE /microposts/:id(.:format) microposts#destroy

解决问题

通过输出可以看出create动作使用post向microposts发出,而destroy动作使用delete向micropost发出。

可以修改测试代码:

post microposts_url(micropost: { content: "Lorem ipsum" })
delete micropost_url(@micropost)

总结

至此,问题解决了,遇到类似的情况时同样可行。碰到问题是不要急躁,耐心查看总能解决。

你可能感兴趣的:(ror:路由配置/查看/使用)