I am trying to use a form input from a rails controller in a Python script. I have a form on my rails (version 4.2.1) app that take in a url, then I want to use that url in a Python script. I'm new to rails and have no idea how to do this. My app has gotten as far as taking in the form inputs and rendering them on a page as well as being able to call a Python script and run it, but I need to link them together.
Here is the controller code so far:
class ContestsController < ApplicationController
def index
value = %x(python /Users/my/Desktop/rails_test.py 2>&1)
render :text => value
@contests = Contest.all
end
def new
@contest = Contest.new
end
def create
@contest = Contest.new(contest_params)
if @contest.save
redirect_to contests_url
else
render 'new'
end
end
private
def contest_params
params.require(:contest).permit(:site, :contest_url)
end
end
My Python rails_test.py
script is:
#!/bin/bash
print "Python script works!"
#url = last :contest_url param from rails app
#print url
Try #1:
I modified the rails code to:
value = %x(python /Users/jdesilvio/Desktop/rails_test.py #{Shellwords.escape(params[:contest_url])} 2>&1)
I modified the Python script to:
#!/Users/me/anaconda/bin/python2.7 python
import sys
print "Python script works!"
print "Url: ", sys.argv[1]
The output is:
Python script works! Url:
My form is:
<%= form_for @contest do |f| %>
<div>
<%= f.label :site %>
<%= f.text_field :site %>
</div>
<div>
<%= f.label :contest_url %>
<%= f.text_field :contest_url %>
</div>
<%= f.submit %>
<% end %>