2010-12-28 2 views
0

레일 프로젝트 내부에서 셸 명령을 실행하려고하면 환경과 같은 이음새가로드되지 않습니다.레일 셸 명령 - 레이크를 찾을 수 없음

나는 이런 식으로 문제를 해결 :

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` 

더 좋은 방법이 있나요?

답변

2

왜 Rails 프로젝트에서 Rake를 쉘로 실행하려고합니까? 모든 작업을 수행하는 수업을 만드십시오.

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

당신은 아주 쉽게 레이크에서이를 사용할 수 있습니다

# 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 

당신은 또한 같은 컨트롤러로, 아주 쉽게 레일즈 프로젝트에 다른 곳에서이를 사용할 수 있습니다.

# 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