我有来自Rails上的ruby的代码,我想使用rails实现Digitalocean液滴的自动创建,因此当我从视图创建add new并添加所有详细信息时,它应该在后端触发一个事件。
在这里输入我的代码
gem文件
gem 'droplet_kit'
config/initializer/digitalocean.rb
require 'droplet_kit'
client = DropletKit::Client.new(access_token: ENV['digitalocean_token'])
在我的控制器中
def create
@hosting = Hosting.new(hosting_params)
droplet = DropletKit::Droplet.new(hosting_params)
created = client.droplets.create(droplet)
respond_to do |format|
if @hosting.save
format.html { redirect_to @hosting, notice: 'Hosting was successfully created.' }
format.json { render :show, status: :created, location: @hosting }
else
format.html { render :new }
format.json { render json: @hosting.errors, status: :unprocessable_entity }
end
end
end
我想做的是创建小滴,但要注意。但是错误是
uninitialized constant HostingsController::DropletKit
答案 0 :(得分:0)
在阅读了大量评论和文章后,我找到了对这个问题的答案。 Here中找到了这篇文章,这就是我的工作
config/initializer/digitalocean.rb
require 'droplet_kit'
# In Rails, you could put this in config/initializers/digitalocean.rb
DropletKit.configure do |config|
config.access_token = ENV['digitalocean_token']
config.user_agent = 'custom'
end
将常量添加到控制器
def create
@hosting = Hosting.new(hosting_params)
client = DropletKit::Client.new(access_token: ENV['digitalocean_token'])
droplet = DropletKit::Droplet.new(
name: 'mysite.com',
region: 'nyc3',
image: 'ubuntu-14-04-x64',
size: 's-1vcpu-1gb'
)
created = client.droplets.create(droplet)
respond_to do |format|
if @hosting.save
format.html { redirect_to @hosting, notice: 'Hosting was successfully created.' }
format.json { render :show, status: :created, location: @hosting }
else
format.html { render :new }
format.json { render json: @hosting.errors, status: :unprocessable_entity }
end
end
end
就是这样。