我正在尝试在Heroku上使用Paperclip和SWS S3来让用户上传图片。
我关注this tutorial以及创建新用户时的内容
ArgumentError in UsersController#create
app/controllers/users_controller.rb:28:in `create'
我对它一无所知
users_controller.rb
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to bequem!"
redirect_to @user
else
render 'new'
end
end
new.html.erb
<div class="col-md-4 col-md-offset-4">
<%= provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :avatar %><br />
<%= f.file_field :avatar %>
-->
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", :class => "btn btn-large btn-primary" %>
<% end %>
</div>
关于生产和开发的回形针配置
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['xxx'],
:access_key_id => ENV['xxx'],
:secret_access_key => ENV['xxx']
}
}
用户模型
class User < ActiveRecord::Base
# This method associates the attribute ":avatar" with a file attachment
has_attached_file :avatar, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
attr_accessible :name, :email, :password, :password_confirmation, :avatar
papertrail log
Started GET "/signup" for 176.226.131.101 at 2014-01-20 17:45:54 +0000
Jan 20 09:45:55 milker app/web.1: Processing by UsersController#new as HTML
Jan 20 09:45:55 milker app/web.1: Rendered shared/_error_messages.html.erb (0.6ms)
Jan 20 09:45:55 milker app/web.1: Rendered users/new.html.erb within layouts/application (164.7ms)
Jan 20 09:45:55 milker app/web.1: Rendered layouts/_shim.html.erb (0.0ms)
Jan 20 09:45:55 milker app/web.1: Completed 200 OK in 199ms (Views: 168.1ms | ActiveRecord: 15.3ms)
Jan 20 09:46:11 milker app/web.1: Started POST "/users" for 176.226.131.101 at 2014-01-20 17:46:11 +0000
Jan 20 09:46:11 milker app/web.1: Processing by UsersController#create as HTML
Jan 20 09:46:11 milker app/web.1: Parameters: {"utf8"=>"✓", "authenticity_token"=>"5KZg5kTVOWKN/VBFlw6BC5pJe1EGCBYtVN5zBXLXNT4=", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f4f673ad6b8 @original_filename="3adPUTPPeg8.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"3adPUTPPeg8.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20140120-2-n87ap6>>, "name"=>"slasher", "email"=>"slava.proshechkin@softeica.ru", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
Jan 20 09:46:11 milker app/web.1: Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:11 milker app/web.1: Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:11 milker app/web.1: Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:11 milker app/web.1: Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:11 milker app/web.1: Command :: convert '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' -auto-orient -resize "100x100>" '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-k3pl9m'
Jan 20 09:46:11 milker app/web.1: Command :: file -b --mime '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-k3pl9m'
Jan 20 09:46:11 milker app/web.1: Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:12 milker app/web.1: Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:12 milker app/web.1: Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:12 milker app/web.1: Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:12 milker app/web.1: Command :: convert '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' -auto-orient -resize "200x" -crop "200x200+0+33" +repage '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-1acel98'
Jan 20 09:46:12 milker app/web.1: Command :: file -b --mime '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-1acel98'
Jan 20 09:46:12 milker app/web.1: Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:12 milker app/web.1: Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:12 milker app/web.1: Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:12 milker app/web.1: Command :: identify -format %m '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]'
Jan 20 09:46:12 milker app/web.1: Command :: convert '/tmp/3adPUTPPeg820140120-2-yc1128.jpg[0]' -auto-orient -resize "300x300>" '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-1fz8qbp'
Jan 20 09:46:12 milker app/web.1: Command :: file -b --mime '/tmp/3adPUTPPeg820140120-2-yc112820140120-2-1fz8qbp'
Jan 20 09:46:12 milker app/web.1: [paperclip] saving /users/avatars/000/000/007/original/3adPUTPPeg8.jpg
Jan 20 09:46:13 milker heroku/router: at=info method=POST path=/users host=milker.herokuapp.com request_id=97266e1a-098b-4676-9ad2-791e85d25f43 fwd="176.226.131.101" dyno=web.1 connect=14ms service=2457ms status=500 bytes=643
Jan 20 09:46:13 milker app/web.1: Completed 500 Internal Server Error in 1653ms
Jan 20 09:46:13 milker app/web.1: ArgumentError (missing required :bucket option):
Jan 20 09:46:13 milker app/web.1: app/controllers/users_controller.rb:28:in `create'
任何想法?
提前谢谢你
答案 0 :(得分:2)
继manojild的回答之后,这就是你需要做的事情(我们以前遇到过这个问题):
<强>模型强>
不知道为什么,但您必须在:bucket
模型中加入Paperclip
选项:
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "************",
:storage => :s3,
:bucket => 'bucket_name',
:s3_credentials => S3_CREDENTIALS (we set this in an initializer)
ENV Vars
您在ENV
development.rb
个变种
除非您在操作环境中实际设置了这些VAR
,否则您将无法访问它们。我建议使用Figaro
gem来处理开发中的ENV变量&amp;生产,保持一致性
你真的希望你的ENV变量是这样的:
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
你有他们当前的实际内容
答案 1 :(得分:1)
从你的代码:
:bucket => ENV['BEQUEM'],
:access_key_id => ENV['AKredacted'],
:secret_access_key => ENV['AKredacted']
完全按照教程。它要求你添加:
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
这些是环境变量,将使用heroku config
命令进行设置。您已将这些替换为实际值(并可能通过在公开场合公开它们而导致安全问题)
如教程中所述,从您的控制台运行以下命令:
$ heroku config:set S3_BUCKET_NAME=your_bucket_name
$ heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
$ heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key
不要忘记将your_bucket_name
等替换为此处的实际值:)
答案 2 :(得分:0)
工作用户模型。计划将来使用Fedora。
has_attached_file :avatar, :styles => { :small => '60x60#', :large => '300x300#' }, :default_style => :large,
:storage => :s3,
:default_url => '/images/:attachment/missing_:style.png',
:path => "users/:id/avatar/:style.:extension",
:bucket => "bequem",
:s3_credentials => {
:access_key_id => "xxx",
:secret_access_key => "xxx"
}
答案 3 :(得分:0)
将它添加到模块和类中的application.rb文件中:
f