Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
206 lines (126 sloc) 8.73 KB


Rails 4 support is currently being tested. To try it out, just use Turkee's master branch. In your Gemfile, add the following :

gem 'turkee', :git => '', branch: 'master'


  • Perform user feedback studies easily.

  • Seamlessly convert your Rails forms for use on Mechanical Turk.

  • Easily import the data posted by the Mechanical Turk workers back into your data models.

External forms are created using a simple form helper. HITs are created by issuing a rake command. Retrieving submitted response data and importing that data into your model(s) requires just one more rake command.


Add turkee to your Gemfile as a gem dependency, then do a 'bundle install':

gem 'turkee'

If you're upgrading Turkee (1.1.1 and prior) or installing for the first time, run:

rails g turkee --skip

(the skip flag will ensure that you don't overwrite prior Turkee initializers and migrations.)

This will copy the needed migrations and config/initializer into your application directory.

To access the Turkee rake tasks, add the following to your application's Rakefile:

require 'tasks/turkee'

If you haven't created a Mechanical Turk account, surf on over to Amazon’s Web Services and create an AWS account.

Once you have your account created, you can access your AWS access key and secret access key from here.


Inside the config/initializers directory, you'll see the file turkee.rb. Edit the file with your Amazon credentials.


RTurk::logger.level     = Logger::DEBUG
RTurk.setup(AWSACCESSKEYID, AWSSECRETACCESSKEY, :sandbox => (Rails.env == 'production' ? false : true))

Run the migrations :

rake db:migrate


Mechanical Turk will open your forms in an iFrame from a secure URL (ssl). If you test locally with localhost, you're going to need to be running SSL so that you don't receive a mixed content warning within Chrome and a blank screen. Thin allows for local SSL.

# Gemfile
gem 'thin', :group => :development

Then you can start the server by calling :

thin start --ssl


1) You should disable form controls if the Turker hasn't accepted the HIT. You can determine this from your controller:

class SurveysController < ApplicationController

    def new
        @disabled      = Turkee::TurkeeFormHelper::disable_form_fields?(params)

        # If you wanted to find the associated turkee_task, you could do a find by hitId
        #  Not necessary in a simple example.
        # @turkee_task   = Turkee::TurkeeTask.find_by_hit_id(params[:hitId]).id rescue nil

        @survey        =

2) Change your forms to use the form helper.

<%= turkee_form_for(@survey, params) do |f| %>
    <p><%= f.text_area :value, :disabled => @disabled %></p>
    <p><%= f.submit 'Create', :disabled => @disabled %></p>
<% end %>

Using the turkee_form_for helper will post the form to the Mechanical Turk sandbox if you're in development/test mode, otherwise it will be posted to Mechanical Turk production/live site.

3) Run the following rake task to post to Mechanical Turk :

# Host URL of your application
# Title of your HIT
# Description of your HIT
# Model name of your task form (the New action should be implemented)
# Number of assignments for HIT
# The reward for a successful completion
# The lifetime of the HIT in days (e.g. 5 days)

rake turkee:post_hit[<Host>, <Title>, <Description>, <Model>, <Number of Assignments>, <Reward>, <Lifetime>]

e.g. :
rake turkee:post_hit["","Please complete our survey","Tell us your favorite color.","Survey",100,0.05,5,1]
** Do not put spaces before or after commas for the rake task parameters

This will insert a row for the requested HIT into the turkee_tasks table. The turkee_tasks entry stores (along with the other properties) the HIT URL (e.g. ) and HIT ID (e.g. 1J1EXO8SUQ3URTTUYGHJ7EKUT11 ). These values are returned from Mechanical Turk when the HIT request is posted.

When a Turk worker views your HIT, the HIT will display your form within an iFrame. With the above example, Mechanical Turk will open an iFrame for the HIT assignment displaying the form


1) Add the following method call to your app/views/layouts/application.html.haml file :

<%= turkee_study %>

2) Call the create_study rake task :

rake turkee:create_study[<Application Page URL>, <HIT Title>, <HIT Description>, <Number of Assignments>, <Reward>, <Lifetime of HIT>]

# 30 Assignments at a quarter each requesting feedback on my site
RAILS_ENV=production rake turkee:create_study['',"Can you help me test my website?","Give feedback below on what you didn't like about my site. Click submit below when you're finished.", 30,0.25, 20]

3) The Turker will see a feedback form overlayed over your website. Within this textarea, they can provide feedback.



1) Run the rake task that retrieves the values from Mechanical Turk and stores the user entered values into your model.

rake turkee:get_all_results

Rerun this task periodically to retrieve newly entered form values. You can setup this task as a cronjob to automate this.

crontab -e

# E.g. run every five minutes
*/5 * * * * cd /var/www/your/turk/application && rake turkee:get_all_results

Or you can directly call :



1) When a response is retrieved from Mechanical Turk, Turkee attempts to create a data row for the model specified using the corresponding retrieved data. If the row cannot be created (input failed model validations), the assignment is rejected. As for Mechanical Turk approval, if the row is created and you haven't specified your own custom approve? method for the model, the assignment will automatically be approved. If you'd like to add your own custom approval method, add the approve? instance method to your model. E.g. :

class Survey < ActiveRecord::Base
  def approve?
    (!response.blank? && !comment.blank?)

2) When all specified assignments for a HIT have been completed, Turkee will attempt to call the optional hit_complete class method for the model. This can be used for any cleanup logic. E.g. :

class Survey < ActiveRecord::Base
  def self.hit_complete(turkee_task)
    #do something

3) If all of specified assignments for a HIT have not been completed by the end of the HITs lifetime, Turkee will attempt to call the optional hit_expired class method for the model. This can be used for any cleanup logic. E.g. :

class Survey < ActiveRecord::Base
  def self.hit_expired(turkee_task)
    #do something

Advanced Usage

1) You can use the params hash to pass object IDs to your forms. E.g. if you wanted to setup a form of questions about a given URL (let's call the model UrlSurvey), your code would look something like :

URL.all do |url|
    Turkee::TurkeeTask.create_hit(host, "Enter the address displayed for the given url.",
        hit_description, UrlSurvey, num_assignments, reward,
        lifetime, duration, {}, {:url_id =>}, {})

Then when displaying your form, you can find the URL object via the params[:url_id] value.

2) You can pass in qualifications to allow only certain Turkers to work on your HIT.

E.g. qualifications = {:approval_rate => {:gt => 70}, :country => {:eql => 'US'}}

Turkee::TurkeeTask.create_hit(host, hit_title, hit_description, UrlSurvey, num_assignments, reward, lifetime, duration, qualifications, {}, {})
  • The rake task does not support the passing of qualifications.

3) Turkee assumes that the form url will be the new action for the class passed to create_hit. If you have a more complex form url which would be the case for nested resources, you can use the :form_url option to designate a form url different from the default.

form_url = Rails.application.routes.url_helpers.send("new_user_survey_path",@my_survey)

Turkee::TurkeeTask.create_hit(host, hit_title, hit_description, :Survey, num_assignments, reward,
    lifetime, duration, {}, {}, {:form_url => form_url})


Copyright © 2010 - 2012 Jim Jones. See LICENSE for details.

Jump to Line
Something went wrong with that request. Please try again.