2013-02-13 25 views
0

已解决:我做了一些错误的事情,所有这些都涉及我的控制器接收数据。关于发送数据的方法没有任何问题。如何通过外部RESTful呼叫插入RAILS db

1:我不使用@ report.save在我reportController#创建

2:我不传递PARAMS [:报告]在我的控制器

3:我添加 “skip_before_filter:verify_authenticity_token”到我的应用程序控制器来停止日志中的警告。 解决。数据插入成功。

===== ORIG。下面的问题=====

我需要一个外部程序来发布一个命令,将东西插入Ruby on Rails数据库。 我明白这一点的安全含义,但由于这个应用程序并非面向公众,所以它不是真正的问题。

这是我期待实现工作流程:

REST客户端>扶手>创建新的数据库表行

为了举例:我route.rb文件包含

resources :reports 

所以我能够使用这些路线CRUD。我似乎不能让我的休息客户端正常工作。

更新: 我试过一个RUBY休息客户端和卷曲命令在一个,无济于事。

require 'rest_client' 
require 'json' 

hash_to_send = {:test_name => 'Fake Name', :pass_fail => 'pass',:run_id => 1111, :category => 'Fake Category'} 

#formulate CURL attempt 
myCommand = "curl -H 'Content-Type: application/json' -X POST http://localhost:8889/report.json -d #{hash_to_send.to_json} > deleteme.html" 
#execute CURL attempt 
`#{myCommand}` # RESULT--> 795: unexpected token at 'test_name:Fake Name' 


#Make Ruby rest client attempt 
response = RestClient.post("http://localhost:8889/report.json", 
    hash_to_send.to_json, 
    :content_type => :json, :accept => :json 
) 

#debug info 
puts myCommand # Returns --> {"test_name":"Fake Name","pass_fail":"pass","run_id":1111,"category":"Fake Category"} 
+0

解决:我做了几件事情是错误的。 1:没有在我的reportController中使用@ report.save#create – Pandem1c 2013-02-14 00:37:31

回答

1

而是在命令行卷曲,使用Ruby脚本和处理由宝石REST调用和JSON的转换。例如,使用rest-client宝石(https://github.com/archiloque/rest-client)和标准json宝石,你可以写:

require 'rest_client' 
require 'json' 

response = RestClient.post("http://localhost:8889/report.json", 
    params_in_hash.to_json, 
    { :content_type => :json, :accept => :json } 
) 
+0

我意识到这一点,并试图无济于事。但是错误不是描述性的。 这里是我写的 '需要 'rest_client'' '需要' json'' 'hash_to_send = {:TEST_NAME => “假名”,:pass_fail => “通行证”}'' 响应= RESTClient实现.POST( “HTTP://本地主机:8889/report.json”, hash_to_send.to_json, :CONTENT_TYPE =>:JSON,:接受=>:JSON ) '' 提出response.inspect' 结果是500错误。 – Pandem1c 2013-02-13 19:42:55

+0

对不起,我不知道如何在响应中格式化我的代码。反引号似乎不工作,如“帮助”中所述 – Pandem1c 2013-02-13 19:47:02

+0

糟糕,我误导了你。尝试将HTTP标头放在哈希中:'{:content_type =>:json,:accept =>:json}'(原始文章已更新) – 2013-02-13 22:39:11