2016-08-04 47 views
-1

我是新手和學習導軌 我已經設置了我的aws帳戶,創建了一個存儲桶和IAM。我還將所需的策略附加到我的IAM上。 但是我無法上傳圖片。導軌教程13.4.4

if Rails.env.production? 
CarrierWave.configure do |config| 

config.fog_credentials = { 
    # Configuration for Amazon S3 
    :provider    => 'AWS', 
    :aws_access_key_id  => ENV['S3_ACCESS_KEY'], 
    :aws_secret_access_key => ENV['S3_SECRET_KEY'] 
} 
config.fog_directory  = ENV['S3_BUCKET'] 
end 
end 



class PictureUploader < CarrierWave::Uploader::Base 
include CarrierWave::MiniMagick 
process resize_to_limit: [400, 400] 

if Rails.env.production? 
storage:fog 
else 
storage:file 
end 

def store_dir 
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
end 

def extension_white_list 
    %w(jpg jpeg gif png) 
end 

end 

這些都是我的日誌:
enter image description here

編輯,這是我的micropost_controller.rb

class MicropostsController < ApplicationController 
    before_action :logged_in_user, only: [:create, :destroy] 
    before_action :correct_user, only: [:destroy] 

    def create 
    @micropost = current_user.microposts.build(micropost_params) 
    if @micropost.save 
     flash[:success] = "Micropost created!" 
     redirect_to root_url 
    else 
     @feed_items = [] 
     render 'static_pages/home' 
    end 
    end 

    def destroy 
    @micropost.destroy 
    flash[:success] = "Micropost deleted" 
    redirect_to request.referrer || root_url 
    end 

    private 

    def micropost_params 
    params.require(:micropost).permit(:content, :picture) 
    end 

    def correct_user 
     @micropost = current_user.microposts.find_by(id: params[:id]) 
     redirect_to root_url if @micropost.nil? 
    end 
end 

THX

+0

它看起來像AWS上的政策問題。當我跟着那篇教程時,我記得我創建了一個組,爲其分配了所需的策略,然後將一個特定的用戶(由應用程序使用)添加到組中。無論如何,請將'microposts_controller.rb'添加到您的問題中,以便我們檢查創建操作。 – davideghz

+0

此外,根據您使用的是哪個AWS區域,您可能需要在'config.fog_credentials'哈希中將':region => ENV ['S3_REGION']'添加到'carrier_wave.rb'初始化程序中。 – davideghz

+0

你的控制器看起來不錯。你有沒有嘗試添加區域配置到fog_credentials? – davideghz

回答

0

我想通了,我的問題,它是在Heroku的。我的配置變量S3_BUCKET實際上是S3__BUCKET,我很傻。 THanks戴夫