3

我想用回形針將圖像上傳到亞馬遜S3使用heroku。到目前爲止,我得到:警告:無法爲用戶:圖片批量分配受保護的屬性。我經歷過很多指導,沒有任何幫助。警告:不能批量分配受保護的屬性Paperclip Rails 4

我的模型:

class User < ActiveRecord::Base 
    resourcify 
attr_accessible :login, :password, :password_confirmation, :longitue, :latitude, :showingName,:email,:contato,:telefone 
has_many :api_keys 
has_secure_password 

    has_attached_file :picture, 
           styles: { 
            thumb: '100x100>', 
            square: '200x200>', 
            medium: '300x300>', 
            icon: '30x30>'}#, 


    validates_attachment_content_type :picture, :content_type => /\Aimage\/.*\Z/ 
    validates_attachment_file_name :picture, :matches => [/png\Z/, /jpe?g\Z/] 
    validates_with AttachmentSizeValidator, :attributes => :picture, :less_than => 3.megabytes 

    rolify :before_add => :before_add_method 

    def before_add_method(role) 
     # do something before it gets added 
end 

我的控制器:

class UsersController < ApplicationController 
load_and_authorize_resource 

    def new 
    end 

    def index 
    end 

    def create 
    @user = User.new(user_params) 
    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'Comida was successfully created.' } 
     format.json { render :show, status: :created, location: @user } 
     else 
     format.html { render :new } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 

    end 

private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_user 
     @user = User.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def user_params 
     params.require(:user).permit(:picture,:login, :password, :longitue, :latitude, :showingName,:email,:contato,:telefone) 
    end 


end 

我的觀點:

<%= simple_form_for @user, :html => { :class => 'form-horizontal', :multipart => true } do |f| %> 


    <%= f.input :nome %> 


<%= f.label :picture %> 
    <%= f.file_field :picture %> 

    <%= f.input :showingName %> 
    <%= f.input :email %> 
    <%= f.input :login %> 

    <%= f.input :password %> 
    <%= f.input :password_confirmation %> 
    <%= f.input :contato %> 
    <%= f.input :telefone %> 
    <%= f.input :description %> 
    <%= f.input :laitude %> 
    <%= f.input :logitude %> 
    <div class="form-actions"> 
    <%= f.button :submit, :class => 'btn-primary' %> 
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")), 
       users_path, :class => 'btn btn-meubar' %> 
    </div> 
<% end %> 

production.rb

config.paperclip_defaults = { 
    :storage => :s3, 
    :s3_host_name => 's3-sa-east-1.amazonaws.com', 
    :s3_credentials => { 
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'], 
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'], 
    :bucket => ENV['S3_BUCKET_NAME']  
    }, 
    :path => ":class/:id/:basename_:style.:extension", 
    :url => ":s3_path_url" 
} 

感謝您的時間和幫助:)

+0

也許這將有助於:http://stackoverflow.com/a/13444458/2399861,結合@ user860478的回答 –

+0

剛剛添加我的production.rb。這似乎是好的 –

回答

1

只需將:picture添加到attr_accessible即可。

+0

我試圖做到這一點,我收到以下錯誤:「缺少必需:桶選項」 –

+0

'缺少必需:桶選項「'是可以通過運行修復下一個/單獨的問題'heroku config:add S3_BUCKET_NAME = S3中的存儲桶名稱';'production.rb'中的配置文件正在尋找要設置的env變量。您可以從s3配置中獲取存儲桶名稱。 –

+0

根據回形針文檔,我應該使用att_accessible:圖片。 –

相關問題