0

I am trying to execute a curl command inside my rails application. My rails application simply creates a ticket and for that I have to enter some information.

When person clicks on Save then it saves the data entered into DB. Now I am trying to run a curl script whenever a user clicks on Save.

**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 

when I click on Save, it enters data into my DB and shows this in the log file:

 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)

Now I have a curl script which actually enters data into a different project:

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/'

It works perfectly fine and enters data into the another project when I run this on command prompt. Now I am merging this code into my rails project (tickets) and whenever person clicks on the save button, this script should execute and enters data into the another project.

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 

Now when I run this application , and clicks on the save button , it creates the ticket and enter the ticket data into rails_application DB successfully. But it does not successfully execute this curl script and enters data into the other project.

here is the log file:

 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)

EDIT

Hey, now its running that query and entering data into the other project.. can any one suggest me that is there a way I can use the variables instead of hardcoded value in the script:

 %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/' }

In this script how can I use the the values which I enter for my tickets rails project, Actually i want to fetch the information which users enters into the text fields on my rails project to another project.. whenever user clicks on save the information which he has entered should go to another project DB.

variables in my rails project:

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

You almost have it. You need to change the single quotes to double quotes so you can use ruby interpolation, then escape all the quotes in the curl so that they get interpolated into the final string. It is ugly, but looks something like this:

 %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/' }

You may also be able to do it a little more neatly like this:

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/']

I am unable to completely this, so you may need to juggle quote marks a little more, but this is the idea.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.