Ruby on rails 在rails应用程序中运行rake任务

Ruby on rails 在rails应用程序中运行rake任务,ruby-on-rails,ruby,ruby-on-rails-3.1,asset-pipeline,rake-task,rails,gemfile,Ruby On Rails,Ruby,Ruby On Rails 3.1,Asset Pipeline,Rake Task,Rails,Gemfile,我想在rails应用程序中运行资产预编译任务,因为我有许多依赖项,这些依赖项会更改代码,在这种情况下,每当代码更改时,我都需要运行脚本,因为我无法授予服务器访问权限,所以我为他们提供GUI,这样他们就可以单独运行脚本,所以,我已经构建了UI来运行带有一些参数的任务,比如 system("Template='#{params[:template]}' Theme='#{params[:theme]}' rake assets:precompile) 我从UI(params[:template]、

我想在rails应用程序中运行资产预编译任务,因为我有许多依赖项,这些依赖项会更改代码,在这种情况下,每当代码更改时,我都需要运行脚本,因为我无法授予服务器访问权限,所以我为他们提供GUI,这样他们就可以单独运行脚本,所以,我已经构建了UI来运行带有一些参数的任务,比如

system("Template='#{params[:template]}' Theme='#{params[:theme]}' rake assets:precompile)
我从UI(params[:template]、params[:theme])中获得两个值。另外,我希望在另一个路径(站点路径)中运行此任务,这意味着管理员端UI存在,任务应在站点目录中执行

  if(params[:theme_script] == "true")
      template=Template.where(:name => params[:template]).first
     if template
      theme = template.themes.where(:name => params[:theme]).first
      if theme
    #   Dir.chdir "#{THEMEPATH}"do
    #     `Template="#{template.name}" Theme="#{theme.name}" rake assets:precompile`
    #   end
    #      sleep 10
    #      system("#{Rails.root.to_s}/lib/shell_script.sh")
    #      RunRake.run_rake(template.name,theme.name)
    #   Dir.chdir "#{THEMEPATH}"do
    #     Rake::Task['assets:precompile'].invoke
    #   end
          ENV["Template"] = template.name
          ENV["Theme"] = theme.name
          precompile_task = "bundle exec rake assets:precompile --trace 2>&1"
          output = Dir.chdir(THEMEPATH) { %x[ #{precompile_task} ] }
          flash[:notice] = "Asset created successfully"
      else
        flash[:notice] = "U have enter invalid data"
      end
    else
      flash[:notice] = "U have enter invalid data"
    end
  end
这是我的代码,我正在检查多个条件并允许执行任务

我已经通过放入控制器和库来尝试这段代码,但这不起作用

我也尝试过使用shell脚本


任何人都可以帮助我。

您可以为设置一个环境变量,然后从控制器发出
#invoke
方法。因此,准备文件:

gem 'rake'
config/initializers/rake.rb:

Rake.load_rakefile Rails.root.join( 'Rakefile' )
ENV["Template"] = template.name
ENV["Theme"] = theme.name
Rake::Task[ 'assets:precompile' ].invoke
#!/bin/bash

source "$HOME/.rvm/scripts/rvm"
cd /other/app/path
export Template="$1"
export Theme="$2"
rake assets:precompile
应用程序/控制器/您的控制器:

Rake.load_rakefile Rails.root.join( 'Rakefile' )
ENV["Template"] = template.name
ENV["Theme"] = theme.name
Rake::Task[ 'assets:precompile' ].invoke
#!/bin/bash

source "$HOME/.rvm/scripts/rvm"
cd /other/app/path
export Template="$1"
export Theme="$2"
rake assets:precompile
发布
bundle安装
,然后运行控制台
rails c
,然后键入:

Rake::Task.tasks.map(&:name).grep 'assets:precompile'
# => ["assets:precompile"]
如您所见,任务
assets:precompile
已成功加载。然后只需为控制器发出操作

要为其他应用程序运行任务,您需要同时运行其他实例,类似于您所做的:

system( "other_app_run.sh '#{template.name}' '${theme.name}'" )
其他应用程序运行。sh:

Rake.load_rakefile Rails.root.join( 'Rakefile' )
ENV["Template"] = template.name
ENV["Theme"] = theme.name
Rake::Task[ 'assets:precompile' ].invoke
#!/bin/bash

source "$HOME/.rvm/scripts/rvm"
cd /other/app/path
export Template="$1"
export Theme="$2"
rake assets:precompile

这并不是资产编译的用途。您应该在部署时通过构建脚本自动执行此操作,而不是手动执行。你应该没有理由想要一个GUI。谢谢你的回复,因为我有很多依赖项,他们会更改代码,在这种情况下,每当他们更改时,我都需要运行脚本,因为我无法授予服务器对他们的访问权限,所以我为他们提供GUI,这样他们就可以单独运行脚本。@МаъСъъСъъъъъъ请帮助我如何传递参数?我已经编辑了我的问题,并发布了我的代码。请验证和帮助我。@Kingston好吧,问题是一样的,调用发生了什么?我收到了太多错误,命令失败,状态为(1):[/home/Kingston/.rvm/rubies/ruby-1.9.3-p448/…],应用程序已经初始化,在翻译中出现了一个SystemExit#异常:exit,,。。。。未定义的方法'each_logical_path'用于管理端的nil:NilClass我没有使用资产概念。因此config.assets.enabled=false存在。我认为目录没有更改。如果我在主题端运行任务,则表示任务已成功运行,但我在管理端构建了没有资产概念的GUI,现在,当我试图从任务管理端运行到主题端时,时间目录没有改变,问题是什么呢?