Rails 3 link_to rake task

空扰寡人 提交于 2019-12-13 03:12:17

问题


I've created a rake task in my application and now I want the task to be accesible for app users from a link on a menu, but I don't know how to invoke it from there. Something like this...?

<%= link_to t('backup'), Rake::Task['backup'].invoke %>

回答1:


You can't do it. Link_to can link to something static or controller action. So you need to create some action, where you can invoke your Rake Task.

class MyTasksController < ApplicationController 
  def rake_it
    Rake::Task['backup'].invoke
  end
end

<%= link_to  t("backup"), {:controller => :my_tasks", :action => "rake_it"}



回答2:


I tried as you said, but the next error appears:

NameError (uninitialized constant MyTasksController::Rake)

Edit answer:

I finally could do it by this way:

class MyTasksController < ApplicationController
  def rake_it
    system ('rake backup:db:mysql')
    redirect_to :action => 'index', :controller => '/events'
  end
end


来源:https://stackoverflow.com/questions/7028517/rails-3-link-to-rake-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!