2013-08-01 15 views
0

我一直試圖讓回形針與亞馬遜s3整天工作,而且我剛剛開始非常接近。SongsController中的參數錯誤#創建缺少必需:存儲桶選項

儘管如何解決這個錯誤?我已經將歌曲包含在歌曲模型中,所以我不確定它要求什麼。一旦解決這個問題,它應該可以工作

錯誤:

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 

song.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 

paperclip.rb

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

production.rb和development.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 
+0

你是如何包含ENV變量的? – TheIrishGuy

+0

也許這就是我想念'但我會想像在production.rb和development.rb – Apane101

+0

@TheIrishGuy我應該在這裏做什麼? :) – Apane101

回答

1

在開發和生產線ENV你撥打:

ENV['my bucketname']

在模型:

ENV['bucketname']

而且對環境變量一個好名字是:

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'] } }

+0

感謝@ rmagnum2002,雖然這些只是例子,不透露我的真實桶名稱。上述所有內容的真實姓名都是相同的。 – Apane101

+1

明白了,也在一個應用程序,我正在與亞馬遜上傳我使用這些S3配置只在環境文件,dev和prod,而不是在模型中,在模型中,只是在模型的頂部添加'require'aws/s3''文件 – rmagnum2002

+0

我在模型中的所有內容都是has_attached_file和它的樣式。 – rmagnum2002

相關問題