0

我有一个 Rails 应用程序,用户可以在其中创建票证。

我的应用程序名称:票证用户必须提交一些信息才能创建票证(姓名、座位 ID、地址、价格、电子邮件)。

当用户单击创建按钮时。它创建票证并将数据提交到 mysql 数据库中。这工作正常。

每当有人单击“创建”按钮时,我实际上都想运行一个 curl 脚本。这样我就可以获取数据(姓名、座位 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 文件的位置,我必须传递变量值(名称、座位 ID、地址、价格、电子邮件)

门票控制器.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

1

您可以system按照此处的文档运行任何系统命令:http: //apidock.com/ruby/Kernel/system

将您的 curl 命令放入一个 shell 脚本文件并从命令行运行它,通过命令行传递您需要的任何数据。

但是...考虑到最有可能使用一些 ruby​​ 库来访问这个其他数据库(远程或非远程)的方法,并且以这种方式维护可能更容易。

于 2016-12-21T20:39:25.427 回答