gem 'paperclip', '~> 4.2'
gem 'aws-s3'
gem 'aws-sdk'
to upload images or any other document. I have these two models: employee.rb
class Employee < ActiveRecord::Base
has_many :attachments, as: :attachable
accepts_nested_attributes_for :attachments
end
and in 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
and in my 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
and in my employess/edit.html.erb
<%= f.fields_for :attachments do |attach|%>
<%= attach.file_field :attach, :class => 'btn' %>
<% end %>
Now when am submitting the form am getting this in my params
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"4pq1y2qS0AAbO+C6O1Sp0qmKFFXG2KZtB4mvIgvK3CKJ0CI/5yAYDJArhjsEzu6lSm/LkRfBIJDpSzRZUxp+bQ==", "employee"=>{"email"=>"inventory@adroit.com", "role"=>"INVENTORY", "department_id"=>"6", "designation_id"=>"3", "is_active"=>"1", "attachments_attributes"=>{"0"=>{"attach"=>#, @original_filename="index.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"employee[attachments_attributes][0][attach]\"; filename=\"index.jpeg\"\r\nContent-Type: image/jpeg\r\n">}}, "action"=>"update", "controller"=>"employees", "id"=>"3"}
But its not saving in my database it gives me error NameError (uninitialized constant AWS::S3::Errors):
and my settings in s3.yml is
development:
bucket: "test"
access_key_id: "*************"
secret_access_key: "*************"
production:
bucket: "test"
access_key_id: "*************"
secret_access_key: "*************"
Now please guide where am missing I am not getting it. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire