【发布时间】:2016-11-01 18:19:02
【问题描述】:
我在应用程序控制器中有以下内容:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def api_auth
@api_key = Rails.application.secrets.api_key
@api_secret = Rails.application.secrets.api_secret
@host_id = Rails.application.secrets.host_id
@data_type = 'JSON'
@options = {
body: {
api_key: @api_key,
api_secret: @api_secret,
host_id: @host_id,
data_type: @data_type
}
}
end
end
在我的控制器中,我有...
before_action :api_auth
...
def get_stuff
response = HTTParty.post("api_url", @options)
stuffs = response.parsed_response["stuffs"]
current_stuffs = stuff.all
stuffs.each do |w|
#unless stuff id already exists in DB, then create it
#what about if an attribute of a stuff is updated? How do we capture that? UUID?
unless (current_stuffs.pluck :stuff_id).map(&:to_s).include?(w["id"].to_s)
stuff.create(stuff_id: w["id"], topic: w["topic"], start_time: w["start_time"], join_url: w["join_url"])
end
end
结束
..以及下面的 rake 任务...
desc "Heroku task to get stuff"
task :get_stuff => :environment do
puts "Getting stuff from api..."
session = ActionDispatch::Integration::Session.new(Rails.application)
session.post "/posts/get_stuff"
puts "done."
end
路线正确,运行任务时我没有收到任何错误。但是,当我运行任务时,我的数据库更新操作没有发生。
我知道该操作有效,因为我在调用相同操作的视图中设置了一个简单的 button_to。
【问题讨论】:
标签: ruby-on-rails