Pavel
Pavel

Reputation: 1974

Rails: shell command in controller

I'm trying to run shell command with rake task of another project in rails controller:

def generate
  render json: `cd /Users/user/project && rvm use ruby-2.1.1@gemset && bundle exec rake users:build`
end

The problem is that not working for that task (nothing happening) but if I run rake routes for example it is running well.

Upvotes: 0

Views: 737

Answers (2)

Michelson
Michelson

Reputation: 351

if you are running another ruby project, make sure you clean the current bundler environment before run command. Bundler provides an easy way to tackle this inconvenient by doing:

 Bundler.with_clean_env{   
    render json:  `cd /Users/user/project  && rvm use ruby-2.1.1@gemset && BUNDLE_GEMFILE=path/to/Gemfile bundle exec rake users:build`
 }

take note of BUNDLE_GEMFILE and Bundler.with_clean_env

Best!

Upvotes: 1

Ali Khan
Ali Khan

Reputation: 59

Instead of issuing a system call, can you try this (place the following snippet in your controller file):

require 'rake'

Rake::Task["users:build"]

Upvotes: 0

Related Questions