问题
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