私は、ユーザがチケットを作成できるレールアプリケーションを持っています。ruby on ruby上でカールしています
私のアプリケーションの名前:チケット チケット(名前、座席番号、住所、価格、電子メール)を作成するには、ユーザーが情報を提出する必要があります。
ユーザーが[作成]ボタンをクリックしたとき。チケットを作成し、mysql DBにデータを送信します。それはうまく動作しています。
私が実際に作成ボタンをクリックするたびにカールスクリプトを実行したいとします。私はデータ(name、seat_id、住所、価格、電子メール)を取ってredcap DB(別のプロジェクト)にインポートすることができます。
これはcurlコマンドです:.CSVファイルの場所で、ここで
# 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}
、私は変数の値を渡すために持っている(名前、seat_id、アドレス、価格、電子メール)
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
私は送信ボタンをクリックしたときに、私は、ログファイルでこれを参照してください。
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"}
あなたのredcapプロジェクトは、このCURLを模倣するAPIエンドポイントを公開していますか? –
はい.. Redcapプロジェクトはまったく同じフィールドを持っています。それは私にAPI TOKENを与えてくれました –
それから、コントローラ自体からそのAPI呼び出しを使用してください。それに従う http://stackoverflow.com/questions/1195962/submitting-post-data-from-the-controller-in-rails-to-another-website –