简体   繁体   中英

How can I run controller action with whenever gem in Rails?

for example I have a controller "Posts" and action "create". Can I run it from terminal?

I want to use whenever gem:

every 2.hours do
    # I want to run posts#index
end

How can I do it?

One solution is create a class that execute the create logic:

class PostsController < ApplicationController

  def create
    creator = PostCreator.new(params)
    creator.process
  end
end

So you can use this class in the Whataver 's job:

every 2.hours do
  creator = PostCreator.new({ whatever: 'you want'})
  creator.process
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM