SongsController#create中的ArgumentError缺少必需的:bucket选项



我一整天都在努力让回形针与amazons3一起工作,我已经非常接近了。

尽管如此,我该如何避免这个错误?我已经在歌曲模型中加入了水桶,所以我不确定它的要求是什么。一旦解决了这个问题,它就会起作用。

错误:

ArgumentError in SongsController#create
missing required :bucket option

    respond_to do |format|
      if @song.save
        format.html { redirect_to @song, notice: 'Song was successfully created.' }
        format.json { render action: 'show', status: :created, location: @song }
      else

歌曲.rb

class Song < ActiveRecord::Base
  acts_as_voteable
  belongs_to :user
  has_many :comments, :dependent => :destroy
  has_many :genre_songs
  has_many :genres, through: :genre_songs
has_attached_file :track,
 :storage => :s3,
  :path => '/:class/:attachment/:id_partition/:style/:filename',
  :url => ":s3_domain_url",
  :bucket => ENV['bucketname']

  validates_attachment :track, :presence => true
  validates_presence_of :url
  validates :title, length: { minimum: 10 }
  validates :url, length: { maximum: 300 }
  def self.tagged_with(name)
    Genre.find_by_name!(name).songs
  end
  def tag_list
    genres.map(&:name).join(", ")
  end
  def tag_list=(names)
    self.genres = names.split(",").map do |n|
      Genre.where(name: n.strip).first_or_create!
    end
  end
end

回形针.rb

# config/initializers/paperclip.rb 
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'

生产.rb和开发.rb

config.paperclip_defaults = {
    :storage => :s3,
    :s3_credentials => {
      :bucket => ENV['my bucketname'],
      :access_key_id => ENV['my access key'],
      :secret_access_key => ENV['my secret access key']
    }
  }

show.html.erb

<p id="notice"><%= notice %>

    <p>
    <%= @song.title %> | ( <%= @song.url %> )
    <br />
    <span class="subtext"><span class="votes_<%= @song.id %>"><%= pluralize(@song.votes.count, 'like') %>,</span>
    posted <%= time_ago_in_words(@song.created_at) + " ago" %>
    <small><span class="comments"></small> | <%= pluralize(@song.comments.size, 'comment') %></span></small><br /></span></span>
    </p>
    <p>
    <%= audio_tag (@song.track.url), controls: "controls", alt: "Please use chrome, ie, or safari", preload: :auto %>   
    </p>
    <p>Genres: <%= raw @song.genres.map(&:name).map { |t| link_to t, genre_path(t) }.join(', ') %></p>
    <%#= link_to 'Show', song, class: "button small secondary" %>
    <%= link_to('Edit', edit_song_path(@song), class: "button small secondary") if can? :update, @song %>
    <%#= link_to 'Back', songs_path,  class: "button small secondary" %>
<br /><br />

    <%= render :partial => 'comments/form' %>
    <div class="replies">
    <% unless @song.comments.empty? %>
      <h5><%= pluralize(@song.comments.size, 'comment') %></h5>
    <br />
    <% end %>
    <% if @song.comments.empty? %>
    <p>There are no comments...</p>
    <% else %>

     <div id="comments">
      <% for comment in @song.comments %>
        <div class="comment">
          <strong><%= link_to_unless comment.site_url.blank?, h(comment.author_name), h(comment.site_url) %></strong>
          <em>on <%= comment.created_at.strftime('%b %d, %Y at %H:%M') %></em>
          <%=simple_format comment.content %><hr>
          <p>
              <%= link_to("Edit", edit_comment_path(comment)) if can? :update, @comment %>
            <% end %>
              <%= link_to("Destroy", comment, :method => :delete, :confirm => "Are you sure?") if can? :destroy, @comment %>
          </p>
        </div></div>
    <% end %>
</div></div>

song_controller.rb

class SongsController < ApplicationController
  before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy, :vote_for_song]
  before_action :set_song, only: [:show, :edit, :update, :destroy, :vote_for_song]
  def vote_for
      @song = Song.find(params[:id])
      current_user.vote_for(@song)
      @song.plusminus = @song.votes_for
      @song.save
      respond_to do |format|
        format.js { render 'update_votes' }
      end
  end
  def vote_against
    @song = Song.find(params[:id])
    current_user.vote_against(@song)
    respond_to do |format|
      format.js { render 'update_votes' }
    end
  end
  def new_songs
    @songs = Song.order "id DESC"
  end

  # GET /Songs
  # GET /Songs.json
  def index
    if params[:genre]
      @songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
    else
      @songs = Song.order('plusminus').paginate(:page => params[:page], :per_page => 15)
    end
  end
  # GET /Songs/1
  # GET /Songs/1.json
  def show
   @comment = Comment.new(song: @song) 
  end
  # GET /Songs/new
  def new
    @song = Song.new
  end
  # GET /Songs/1/edit
  def edit
  end
  # POST /Songs
  # POST /Songs.json
  def create
    @song = Song.new(song_params)
    respond_to do |format|
      if @song.save
        format.html { redirect_to @song, notice: 'Song was successfully created.' }
        format.json { render action: 'show', status: :created, location: @song }
      else
        format.html { render action: 'new' }
        format.json { render json: @song.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /Songs/1
  # PATCH/PUT /Songs/1.json
  def update
    respond_to do |format|
      if @song.update(song_params)
        format.html { redirect_to @song, notice: 'Song was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @song.errors, status: :unprocessable_entity }
      end
    end
  end
  # Song /Songs/1
  # Song /Songs/1.json
  def destroy
    @song.destroy
    respond_to do |format|
      format.html { redirect_to songs_url }
      format.json { head :no_content }
    end
  end
  private
    def set_song
       @song = Song.find(params[:id])
     end
     def song_params
       params.require(:song).permit(:title, :artist, :url, :track, :user_id, :tag_list)
     end
  end

在dev和prod-env中,您可以调用:

ENV['my bucketname']

型号:

ENV['bucketname']

env变量的一个好名字是:

ENV['MY_BUCKETNAME']

在您的情况下:

ENV['AWS_BUCKET']

config.paperclip_defaults = { :storage => :s3, :bucket => ENV['BUCKETNAME'], :s3_credentials => { :access_key_id => ENV['my access key'], :secret_access_key => ENV['my secret access key'] } }

最新更新