I have a rails application where a user can create the tickets.
Name of My application: tickets a user has to submit some information to create the ticket (name, seat_id, address, price, email).
when the user click on the Create button. It creates the ticket and submits the data into the mysql DB. which is working fine.
I actually want to run a curl script whenever a person clicks on the Create button. So that I can take the data (name, seat_id, address, price, email) and import it into redcap DB (another project).
this is the curl command:
# Set secret token specific to your REDCap project
TOKEN="YOUR_TOKEN"
# Set the url to the api (ex. https://YOUR_REDCAP_INSTALLATION/api/)
SERVICE="YOUR_API_URL"
# UPLOAD a flat csv record contain in file file (/path/to/my.csv)
# Note the use of '<' to get curl to read in data from external file
curl --form token=${TOKEN} \
--form overwriteBehavior=normal \
--form content=record
--form format=csv
--form type=flat \
--form data="</path/to/my.csv" \
${SERVICE}
Here at the place of .csv file, I have to pass the variables value (name, seat_id, address, price, email)
tickets_controller.rb
# POST /tickets
# POST /tickets.json
def create
respond_to do |format|
@ticket = Ticket.new(ticket_params)
if @ticket.save
format.html { redirect_to @ticket, notice: 'Ticket was successfully created.' }
format.json { render :show, status: :created, location: @ticket }
# Set secret token specific to your REDCap project
@TOKEN="YOUR_TOKEN"
# Set the url to the api (ex. https://YOUR_REDCAP_INSTALLATION/api/)
@SERVICE="YOUR_API_URL"
# UPLOAD a flat csv record contain in file file (/path/to/my.csv)
# Note the use of '<' to get curl to read in data from external file
system(curl --form token=${@TOKEN} \
--form overwriteBehavior=normal \
--form content=record
--form format=csv
--form type=flat \
--form data="</path/to/my.csv" \
${@SERVICE})
else
format.html { render :new }
format.json { render json: @ticket.errors, status: :unprocessable_entity }
end
end
end
# Use callbacks to share common setup or constraints between actions.
def set_ticket
@ticket = Ticket.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def ticket_params
params.require(:ticket).permit(:name, :seat_id_seq, :address, :price_paid, :email_address,:attachment)
end
whenever I click on the submit button, I see this in the log file:
Started POST "/sparcformpages" for ::1 at 2016-12-21 14:42:32 -0500
Processing by SparcformpagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"8p9NtPkUn9C+YuG3hMQ1LgnKL/8BxEzXHCnV4S7qRNBd8Spwr+jX9Y7E3 qbLok/K4fx/mHFf7Eljo/2UqkHF3w==",
"sparcformpage"=>{"record_id"=>"333", "BSMType"=>"Study Design/Development", "Description"=>"",
"purposeOfStudy"=>"Government Grant Submission", "purposeOfOtherStudy"=>"", "studyDesignSupport"=>"",
"grantNumber"=>"", "purposeOfDataAnalysis"=>"Manuscript Development", "purposeOfOtherAnalysis"=>"",
"typesofAnalysis"=>"Analysis Plan Development", "otherTypesOfAnalysis"=>"", "scopeOfAnalyticSupport"=>"",
"researchType"=>"Human", "IRBNumber"=>"", "IACUCNumber"=>"", "completionDate"=>"", "projectFundingStatus"=>"External funding with built-in BSM support",
"chargeforStudy"=>"", "chargeForDataAnalysis"=>"", "chargeForOtherEffort"=>"", "projectTrainee"=>"Yes", "primaryMentor"=>"", "emailAddress"=>"",
"alreadyCorresponded"=>"Yes", "preferenceOffaculty"=>"Dr. Marni Jacobs", "preferenceOfdataAnalyst"=>"Yao Cheng",
"statusOfCollaboration"=>"ongoing"},
"commit"=>"Create Sparcformpage"}
You can run any system command using
system
as per the doc here: http://apidock.com/ruby/Kernel/systemPut your curl command into a shell script file and run it from the command line, passing in whatever data you need via the command-line.
However... consider that there's most likely a way to use some ruby library to access this other db (remote or not) and it might be easier to maintain that way.