使用 curl 测试 Rails 路由 [英] Using curl to test Rails routes
问题描述
我有一个名为 users 的模型,我想使用 API 更新它.我想这样做的方法是创建一个更新路由并插入执行更新的代码.我使用 RSpec 创建了一个测试,它似乎有效,但是,我想在我的数据库中实际查看更改的数据,因此我尝试使用 curl 来更新我的开发数据库.
我试过命令
curl -X PUT -d "attribute1 = value1, attribute2 = value2" http://localhost:3000/users/1
但是我遇到了错误
<头><title>动作控制器:捕获异常</title><风格>...</风格>头部><身体><h1>无方法错误在用户控制器中#update<pre>没想到你却得到了一个 nil 对象!您可能期望有一个 ActiveRecord::Base 实例.评估 nil.[]
时发生错误....
这是 routes.rb 中的相关行
resources :users, :only =>[:创建,:更新,:销毁]
这是控制器中的相关代码
def 更新@user = User.find(params[:id])if params[:user][:attribute1] == ("x" || "y" || "z")params[:user][:attribute3] = Time.now结尾@user.update_attributes(:user)渲染:什么都没有=>真的结尾
最后是通过的规范代码
describe "PUT 'update'" 做之前(:每个)做@user = 工厂(:用户)结尾描述成功"做它应该成功"做put :update, :id =>@user, :user =>{ :attribute1 =>"x", :attribute2 =>xyz"}response.should be_success结尾它应该更新属性"做old_attribute = @user.attribute3put :update, :id =>@user, :user =>{ :attribute1 =>"x", :attribute2 =>xyz"}@user.attribute3 != old_attribute结尾结尾结尾
我试图找到一个好的 curl 教程来检查我的语法,但我只能找到示例,所以我不太确定语法是否正确.
您的 PUT
数据应如下所示:
user[attribute1]=value1&user[attribute2]=value2
类似于在 URL 的查询字符串上构造参数的方式.
I have a model called users that I want to update using an API. The way I thought to do this is by creating an update route and plugging in code that does the updating. I created a test using RSpec and it seemed to work, however, I want to actually see the changed data in my database so I was trying to use curl to update my development database.
I tried the command
curl -X PUT -d "attribute1 = value1, attribute2 = value2" http://localhost:3000/users/1
but I got an error
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Action Controller: Exception caught</title>
<style>
.
.
.
</style>
</head>
<body>
<h1>
NoMethodError
in UsersController#update
</h1>
<pre>You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]</pre>
.
.
.
Heres's the relevant line in the routes.rb
resources :users, :only => [:create, :update, :destroy]
Here's the relevant code in the controller
def update
@user = User.find(params[:id])
if params[:user][:attribute1] == ("x" || "y" || "z")
params[:user][:attribute3] = Time.now
end
@user.update_attributes(:user)
render :nothing => true
end
And finally here's the spec code that passed
describe "PUT 'update'" do
before(:each) do
@user = Factory(:user)
end
describe "success" do
it "should be successful" do
put :update, :id => @user, :user => { :attribute1 => "x", :attribute2 => "xyz"}
response.should be_success
end
it "should update attribute" do
old_attribute = @user.attribute3
put :update, :id => @user, :user => { :attribute1 => "x", :attribute2 => "xyz"}
@user.attribute3 != old_attribute
end
end
end
I tried to find a good curl tutorial to check my syntax but i could only find examples, so I'm not too sure if the syntax is right.
your PUT
data should look like this:
user[attribute1]=value1&user[attribute2]=value2
Similar to the way you'd construct parameters on the query string of a URL.
这篇关于使用 curl 测试 Rails 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!