Paperclip has_attached_file not working with Rails establish_connection -
i trying access images paperclip using establish_connection.
here code of model article.rb
class article < activerecord::base if rails.env.production? establish_connection secondary_db_config else establish_connection "article_#{rails.env}" end has_many :assets, dependent: :destroy accepts_nested_attributes_for :assets validates_associated :assets end
file asset.rb
class asset < activerecord::base if rails.env.production? establish_connection secondary_db_config else establish_connection "article_#{rails.env}" end belongs_to :article, polymorphic: true has_attached_file :image, :styles => { :large=> "1200x700",:medium => "800x" } validates_attachment_content_type :image, :content_type => ["image/jpg", "image/png", "image/jpeg"] end
and code in view
<% article.all.each |article| %> <div class="project-item col-sm-6 col-md-4 col-lg-3"> <% if article.assets.length > 0 %> <img src="<%= article.assets.last.image.url(:medium) %>" alt="<%=article.name%>" /> <% end %> <div class="hover-title"> <h2 class="project-title"><%= article.name%></h2> <p><%= property.short_desc %></p> </div> </div> <% end %>
it throws error this
undefined method `has_attached_file' asset (call 'asset.connection' establish connection):class
you need install paperclip gem in existing project, copy , configure aws s3 information project need access.
in gemfile
# paperclip gem image manipulation gem 'paperclip', :git=> 'https://github.com/thoughtbot/paperclip', :ref => '523bd46c768226893f23889079a7aa9c73b57d68' # aws sdk uploading @ aws gem 'aws-sdk', '< 2.0' gem 's3'
in production.rb
# configuration amazon s3 config.paperclip_defaults = { :storage => :s3, :s3_region=> env['aws_region'], :s3_credentials => { :bucket => env['aws_bucket'], :access_key_id => env['aws_access_key_id'], :secret_access_key => env['aws_secret_access_key'] } }
Comments
Post a Comment