Rails + Devise + Carrierwave + Carrierwave_direct

时间:2014-01-03 20:21:03

标签: ruby-on-rails devise amazon-s3 carrierwave

我有一个带有devise的Rails 4项目 - 生成users。每个user都有一个avatar,通过carrierwavefog)上传到Amazon S3。我想实现carrierwave_direct以将图像直接上传到S3。

我跟着these steps直到这一步:

  

您可以生成如下的直接上传表单:

class AvatarController < ApplicationController
  def new
    @uploader = User.new.avatar
    @uploader.success_action_redirect = new_user_url
  end
end

<%= direct_upload_form_for @uploader do |f| %>
  <%= f.file_field :avatar %>
  <%= f.submit %>
<% end %>

并且变得困惑。

到目前为止,我有:

  • 假设AvatarController应该在app/controllers/avatar_controller创建(使用与上面相同的代码段)

  • direct_upload_form_for @uploader(上面的完整摘录)放入我的用户“编辑”表单。

我收到错误:

`undefined method `direct_fog_url'`

有没有人知道如何让carrierwave_direct用户devise?一个示例项目会很棒。

2 个答案:

答案 0 :(得分:0)

好像您已按照步骤

进行操作
CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => ENV['AWS_ACCESS_KEY_ID'],
    :aws_secret_access_key  => ENV['AWS_SECRET_ACCESS_KEY']
  }
  config.fog_directory  = ENV['AWS_FOG_DIRECTORY'] # bucket name
end

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWaveDirect::Uploader
end

class UploaderTest < Sinatra::Base
  get "/" do
    @uploader = ImageUploader.new
    @uploader.success_action_redirect = request.url
    haml :index
  end
end

以及

%form{:action => @uploader.direct_fog_url, :method => "post", :enctype => "multipart/form-data"}
  %input{:name => "utf8", :type => "hidden"}
  %input{:type => "hidden", :name => "key", :value => @uploader.key}
  %input{:type => "hidden", :name => "AWSAccessKeyId", :value => @uploader.aws_access_key_id}
  %input{:type => "hidden", :name => "acl", :value => @uploader.acl}
  %input{:type => "hidden", :name => "success_action_redirect", :value => @uploader.success_action_redirect}
  %input{:type => "hidden", :name => "policy", :value => @uploader.policy}
  %input{:type => "hidden", :name => "signature", :value => @uploader.signature}
  %input{:name => "file", :type => "file"}
  %input{:type => "submit", :value => "Upload to S3"}

只有在您的image_uploader.rb中使用storage :fog或者您正在使用Sinatra / haml时,这些仅适用于您... 简单地删除我放入的代码就可以解决问题。

答案 1 :(得分:0)

错误&#34;未定义的方法&#39; direct_fog_url&#39;&#34;可能是因为您正在将活动记录对象提供给该方法而不是载波上传器。

在您的代码中查找您正在使用的&#34; direct_fog_url&#34;并确保@uploader是@ user.avatar而不是@user。

例如,你的工作应该是这样的:

def perform(user_id, key)
  user = User.find user_id
  uploader = user.avatar
  user.key = key
  user.remote_receipt_url = uploader.direct_fog_url(with_path: true)
  user.save!
end