0

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 %>
1

1 Answer 1

2

well you could just pass them as command line arguments to your script. %x does string interpolation, but you need to be careful and verify the input since someone could pass params[:contest_url] = " && rm -rf / " or something similar into your script and cause you problems(never trust user input) So the Shellwords (http://ruby-doc.org/stdlib-2.0/libdoc/shellwords/rdoc/Shellwords.html) class can help. Perhaps something like.

value = %x(/Users/my/Desktop/rails_test.py #{Shellwords.escape(params[:site])} #{Shellwords.escape(params[:contest_url])} 2>&1)

Then just make your python script read the values via STDIN

#!/usr/bin/env python
import sys

print "Site: ", sys.argv[1]
print "Url: ", sys.argv[2]

I made your python scripts shebang call /usr/bin/env python, but if python isn't in the path of the user that your rails app is running as you might need the full path. Also you don't need a /bin/bash at the top of your python script if you are calling it as an argument to the python executable.

Sign up to request clarification or add additional context in comments.

9 Comments

I made the changes you suggested (see above), but it didn't print the argv. Is that how you meant for me to enter the Python path? In my test editor, the curly brackets around #{Shellwords...} won't close, not sure if something is not right in my editor or if there's an issue in the code..
It looks like the parameter is not being passed. When I do: #{Shellwords.escape("test") the output prints 'test'. But when I do #{Shellwords.escape(params[:contest_url])} the out put is ''.
try contest_params[:site]
that's what I thought too, but when I use contest_params[:site] I get the error: param is missing or the value is empty: contest
What does your form look like? Also look in your logs and see what the params hash is getting passed in as. You will need to match that. Did you use a form for? Or a form tag?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.