在 rails 应用程序中运行 curl 命令

在 rails 应用程序中运行 curl 命令

我正在尝试在我的 rails 应用程序中执行 curl 命令。我的 rails 应用程序只是创建一张票,为此我必须输入一些信息。

当用户点击“保存”时,它会将输入的数据保存到数据库中。现在,我尝试在用户点击“保存”时运行 curl 脚本。

**tickets_controller.rb**

Code without Curl Script: 
def create
@ticket = Ticket.new(ticket_params)

respond_to do |format|
  if @ticket.save
    format.html { redirect_to @ticket, notice: 'Ticket was successfully created.' }
    format.json { render :show, status: :created, location: @ticket }
  else
    format.html { render :new }
    format.json { render json: @ticket.errors, status: :unprocessable_entity }
  end
 end
end 

当我单击“保存”时,它会将数据输入到我的数据库中并在日志文件中显示:

 Started POST "/tickets" for 127.0.0.1 at 2017-01-18 10:58:21 -0500
 Processing by TicketsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8L7gmdNZSJ389YpnyYsp4j5F+3lzaTQIP4fo+FZn6h9Qp6u2bA0cXstDLw2vX2fAPHCzKSgijwoDU0UHr+TT2g==", "ticket"=>{"record_id"=>"m2", "seat_id_seq"=>"m2", "address"=>"m2", "price_paid"=>"m2", "email_address"=>"m2"}, "commit"=>"Create Ticket"}
 (0.1ms)  begin transaction
 SQL (0.4ms)  INSERT INTO "tickets" ("record_id", "seat_id_seq", "address", "price_paid", "email_address", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["record_id", "m2"], ["seat_id_seq", "m2"], ["address", "m2"], ["price_paid", "m2"], ["email_address", "m2"], ["created_at", "2017-01-18 15:58:21.219802"], ["updated_at", "2017-01-18 15:58:21.219802"]]
 (181.4ms)  commit transaction
 Redirected to http://localhost:3000/tickets/2
 Completed 302 Found in 189ms (ActiveRecord: 181.9ms)

现在我有一个 curl 脚本,它实际上将数据输入到不同的项目中:

curl -F token='08F14AE57696E458BA6FC6A203F57E69' -F overwriteBehavior=normal -F content=record -F type=flat -F format=json -F data='[{"record_id":"bbc","seat_id_seq":"bbb","address":"bbb","price":"bbb","email":"bbb","tickets1_complete":"2"}]'  'https://cri-datacap.org/api/'

当我在命令提示符下运行此脚本时,它运行良好并将数据输入到另一个项目中。现在,我将这段代码合并到我的 rails 项目 (tickets) 中,每当有人单击保存按钮时,此脚本就会执行并将数据输入到另一个项目中。

def create
@ticket = Ticket.new(ticket_params)

respond_to do |format|
  if @ticket.save
  %x{ curl -F token='08F14AE57696E458BA6FC6A203F57E69' -F overwriteBehavior=normal -F content=record -F type=flat -F format=json -F data='[{"record_id":"bbc","seat_id_seq":"bbb","address":"bbb","price":"bbb","email":"bbb","tickets1_complete":"2"}]'  'https://cri-datacap.org/api/' }
    format.html { redirect_to @ticket, notice: 'Ticket was successfully created.' }
    format.json { render :show, status: :created, location: @ticket }
  else
    format.html { render :new }
    format.json { render json: @ticket.errors, status: :unprocessable_entity }
  end
 end
end 

现在,当我运行此应用程序并单击“保存”按钮时,它会创建票据并将票据数据成功输入到 rails_application DB 中。但它无法成功执行此 curl 脚本并将数据输入到其他项目中。

这是日志文件:

 Started POST "/tickets" for 127.0.0.1 at 2017-01-18 10:56:13 -0500
 Processing by TicketsController#create as HTML
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"foxFimIFUbuuG4OHc2ve5+6melaNL+PtemsM2tOihNTelQ6l3VEFeJmtJu0Vv5DF7JMyBtZkWO9Gv6ElKiG9EQ==", "ticket"=>{"record_id"=>"m1", "seat_id_seq"=>"m1", "address"=>"m1", "price_paid"=>"m1", "email_address"=>"m1"}, "commit"=>"Create Ticket"}
 (0.1ms)  begin transaction
 SQL (96.3ms)  INSERT INTO "tickets" ("record_id", "seat_id_seq", "address", "price_paid", "email_address", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["record_id", "m1"], ["seat_id_seq", "m1"], ["address", "m1"], ["price_paid", "m1"], ["email_address", "m1"], ["created_at", "2017-01-18 15:56:13.416939"], ["updated_at", "2017-01-18 15:56:13.416939"]]
  (243.2ms)  commit transaction
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
100   796  100    12  100   784     30   2000 --:--:-- --:--:-- --:--:--  2000
Redirected to http://localhost:3000/tickets/1
Completed 302 Found in 1039ms (ActiveRecord: 339.6ms)

编辑

嘿,现在它正在运行该查询并将数据输入到另一个项目中。有人可以建议我是否有办法在脚本中使用变量而不是硬编码值:

 %x{ curl -F token='08F14AE57696E458BA6FC6A203F57E69' -F overwriteBehavior=normal -F content=record -F type=flat -F format=json -F data='[{"record_id":"bbc2","seat_id_seq":"bbb","address":"bbb","price":"bbb","email":"bbb","tickets1_complete":"2"}]'  'https://cri-datacap.org/api/' }

在这个脚本中,我如何使用我为票证 Rails 项目输入的值,实际上,我想将用户在我的 Rails 项目的文本字段中输入的信息提取到另一个项目。每当用户单击保存时,他输入的信息都应该转到另一个项目数据库。

我的 Rails 项目中的变量:

    def ticket_params
       params.require(:ticket).permit(:record_id, :seat_id_seq, :address, :price_paid, :email_address)
    end

答案1

您几乎已经搞定了。您需要将单引号更改为双引号,以便使用 ruby​​ 插值,然后转义 curl 中的所有引号,以便将它们插值到最终字符串中。它很丑陋,但看起来像这样:

 %x{ curl -F token='08F14AE57696E458BA6FC6A203F57E69' 
     -F overwriteBehavior=normal -F content=record -F type=flat 
     -F format=json 
     -F data="[{\"record_id\":\"#{record_id}\", \"seat_id_seq\":\"#{seat_id_seq}\", \"address\":\"#{address}\", \"price\":\"#{price_paid}\", \"email\":\"#{email_address}\", "\"tickets1_complete\":\"2\"}]"
     'https://cri-datacap.org/api/' }

您也可以做得更简洁一些,像这样:

require 'json'
h=[{ record_id: record_id, seat_id_seq: seat_id_seq, address: address,
     price: price_paid, email: email_address, tickets1_complete: 2 }]
%x{ curl -F token='08F14AE57696E458BA6FC6A203F57E69' -F
    overwriteBehavior=normal -F content=record -F type=flat 
    -F format=json } +
    ['-F', "data=\"#{h.to_json}\"", 'https://cri-datacap.org/api/']

我无法完全做到这一点,所以您可能需要多加注意引号,但这就是想法。

相关内容