gem 'paperclip', '~> 4.2'
gem 'aws-s3'
gem 'aws-sdk'
上传图片或任何其他文件。我有这两个模型: employee.rb
class Employee < ActiveRecord::Base
has_many :attachments, as: :attachable
accepts_nested_attributes_for :attachments
end
和attachment.rb
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
has_attached_file :attach,
:storage => :s3,
:s3_credentials => '#{Rails.root}/config/s3.yml',
:url => ':s3_domain_url',
:path => '/contents/:id/:basename.:extension'
validates_attachment_content_type :attach,:content_type => ['application/msword','application/pdf', 'image/jpg', 'image/jpeg', 'image/png', 'image/gif']
end
和我的employeeS_controller.rb
def edit
@employee = Employee.where(id: params[:id]).first
@employee.attachments.build
end
def update
@employee = Employee.where(id: params[:id]).first
if @employee.update_attributes(employee_params)
redirect_to @employee, notice: I18n.t('employee_updated')
else
p @employee.errors
render :action => :edit
end
end
private
def employee_params
params.require(:employee).permit(:email, :role, :department_id, :designation_id, :is_active, attachments_attributes: [:attach, :name])
end
和我的就业/ edit.html.erb
<%= f.fields_for :attachments do |attach|%>
<%= attach.file_field :attach, :class => 'btn' %>
<% end %>
现在提交表格的时候我会在我的参数中找到这个
{&#34; utf8&#34; =&gt;&#34;✓&#34;,&#34; _method&#34; =&gt;&#34; patch&#34;, &#34; authenticity_token&#34; = GT;&#34; 4pq1y2qS0AAbO + C6O1Sp0qmKFFXG2KZtB4mvIgvK3CKJ0CI / 5yAYDJArhjsEzu6lSm / LkRfBIJDpSzRZUxp + BQ ==&#34 ;, &#34;员工&#34; =&gt; {&#34;电子邮件&#34; =&gt;&#34; inventory@adroit.com",&#34;角色&#34; =&gt;&#34 ;库存&#34 ;, &#34; department_id&#34; =&gt;&#34; 6&#34;,&#34; designation_id&#34; =&gt;&#34; 3&#34;,&#34; is_active&#34; =&gt; ;&#34; 1&#34 ;, &#34; attachments_attributes&#34; = GT; {&#34; 0&#34; = GT; {&#34;附&#34; = GT;#, @original_filename =&#34; index.jpeg&#34;,@ content_type =&#34; image / jpeg&#34;, @headers =&#34; Content-Disposition:form-data; 命名= \&#34;雇员[attachments_attributes] [0] [附] \&#34 ;; filename = \&#34; index.jpeg \&#34; \ r \ nConContent-Type:image / jpeg \ r \ n&#34;&gt;}}, &#34;操作&#34; =&gt;&#34;更新&#34;,&#34;控制器&#34; =&gt;&#34;员工&#34;,&#34; id&#34; =&gt; ;&#34; 3&#34;}
但它没有保存在我的数据库中它给我错误NameError (未初始化的常量AWS :: S3 ::错误):
我在s3.yml中的设置是
development:
bucket: "test"
access_key_id: "*************"
secret_access_key: "*************"
production:
bucket: "test"
access_key_id: "*************"
secret_access_key: "*************"
当我没有bundle show aws-sdk
时,我有这个版本的aws-sdk捆绑显示aws-sdk
/home/test/vendor/ruby/2.1.0/gems/aws-sdk-2.0.46
现在请指导遗失的地方我没有得到它。提前谢谢。