开发者

Rails shell command - rake not found

开发者 https://www.devze.com 2023-02-01 05:04 出处:网络
It seams like the environment is not loaded when I try to execut开发者_开发知识库e a shell command inside rails project.

It seams like the environment is not loaded when I try to execut开发者_开发知识库e a shell command inside rails project.

I fix it like this:

rcmd = 'rake'
rcmd = '/opt/ruby-enterprise-1.8.7-2010.02/bin/rake' if Rails.env.to_s == 'production'
rcmd = '/usr/local/bin/rake' if Rails.env.to_s == 'staging'
`cd #{Rails.root}; #{rcmd} RAILS_ENV=#{Rails.env} ts:in:delta`

Is there a better way?


Why are you trying to shell out and invoke Rake from within the Rails project? Just make a class that does all the work.

# lib/ts_in_delta.rb
class TsInDelta
  def run
    # code that does all the work here
  end
end

You can use this from Rake quite easily:

# lib/tasks/ts_in_delta.rake
namespace :ts do
  namespace :in do
    task :delta => [:environment] do
      TsInDelta.new.run
    end
  end
end


# shell
$ rake ts:in:delta

You can also use this from anywhere else in your Rails project quite easily, such as from a controller.

# app/controllers/posts_controller.rb (snippet)
class PostsController < ApplicationController
  def ts_in_delta
    TsInDelta.new.run
    render :json => true
  end
end

# config/routes.rb (snippet)
MyApp::Application.routes.draw do
  resources :posts do
    collection do
      post 'ts_in_delta'
    end
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消