[技术博客] rails控制台调试路由

作者:庄廓然

rails console

在项目目录下执行rails console test 可以进入测试模式并且加载测试文件中的yml数据

$ rails console test
Loading test environment (Rails 5.1.7)

rails console 有三种环境,默认是开发环境,还有生产环境和测试环境,以下是参数说明

Usage:
bin/rails console [environment] [options]

Options:
-s, [--sandbox], [--no-sandbox] # Rollback database modifications on exit.
-e, [--environment=ENVIRONMENT] # Specifies the environment to run this console under (test/development/production).

获得路由

app.xxx_path

例如在routes中定义了以下路由:

scope '/:club_id' do
	get '/information', to: 'clubs#information', as: :clubs_information #社团详细信息
end

通过:

> app.clubs_information_path(1)
 => "/clubs/1/information"

发送请求

app.xxx 用来发送请求

get / post / put / delete都可以

app.post('url',params:{key:value})

#等价
app.get('clubs/1/information')
app.get clubs_information_path(1)

获得响应

发送请求后可以用以下语句获得响应

app.response.body #获得响应体
app.response.status #获得响应状态
app.response.headers.inspect
原文地址:https://www.cnblogs.com/buaareadsun/p/10870403.html