警告:无法批量分配受保护的属性 Paperclip Rails 4

4
我正在尝试使用paperclip将图像上传到Amazon S3,使用Heroku。目前我得到了以下警告:WARNING: Can't mass-assign protected attributes for User: picture。我已经看过许多指南,但没有一篇有用。
我的模型:
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"
}

谢谢您的时间和帮助 :)

也许这会有帮助:https://dev59.com/12Yr5IYBdhLWcg3wucew#13444458,结合@user860478的回答。 - Ruby Racer
刚刚添加了我的 production.rb 文件。看起来没问题。 - Henrique Dantas
1个回答

2
只需在attr_accessible中添加:picture即可。

我尝试这样做,但是收到了以下错误提示:“缺少必需的:bucket选项”。 - Henrique Dantas
缺少必需的:bucket选项"是下一个/单独的问题,可以通过运行heroku config:add S3_BUCKET_NAME = <S3中的bucket名称>来解决;production.rb中的配置正在寻找设置环境变量。您可以从S3配置中获取bucket名称。 - Prakash Murthy
根据Paperclip文档,我应该在:picture中使用att_accessible。 - Henrique Dantas
对我来说完美地工作了! - dsomel21

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接