0

ユーザーがチケットを作成できるレールアプリケーションがあります。

私のアプリケーションの名前: ユーザーがチケットを作成するためにいくつかの情報を送信する必要があるチケット (名前、seat_id、住所、価格、電子メール)。

ユーザーが [作成] ボタンをクリックしたとき。チケットを作成し、データを mysql DB に送信します。これは正常に機能しています。

私は実際に、人が [作成] ボタンをクリックするたびに curl スクリプトを実行したいと考えています。データ (名前、seat_id、住所、価格、電子メール) を取得して、redcap DB (別のプロジェクト) にインポートできるようにします。

これはcurlコマンドです:

# 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}

ここでは、.csv ファイルの場所で、変数の値 (name、seat_id、address、price、email) を渡す必要があります。

ticket_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"}
4

1 に答える 1