回形针 has_attached_file 不适用于 Rails establish_connection
Paperclip has_attached_file not working with Rails establish_connection
我正在尝试使用 establish_connection 从回形针访问图像。
这是我的模型代码 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
文件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
代码在视图中
<% Article.all.each do |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 %>
它会抛出这样的错误
undefined method `has_attached_file' for Asset (call 'Asset.connection' to establish a connection):Class
您需要在现有项目中安装回形针gem,从您需要访问的项目中复制并配置AWS S3信息。
在 Gemfile
# paperclip gem for image manipulation
gem 'paperclip', :git=> 'https://github.com/thoughtbot/paperclip', :ref => '523bd46c768226893f23889079a7aa9c73b57d68'
# aws sdk for uploading at AWS
gem 'aws-sdk', '< 2.0'
gem 's3'
在production.rb
# configuration for 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']
}
}
我正在尝试使用 establish_connection 从回形针访问图像。
这是我的模型代码 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
文件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
代码在视图中
<% Article.all.each do |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 %>
它会抛出这样的错误
undefined method `has_attached_file' for Asset (call 'Asset.connection' to establish a connection):Class
您需要在现有项目中安装回形针gem,从您需要访问的项目中复制并配置AWS S3信息。
在 Gemfile
# paperclip gem for image manipulation
gem 'paperclip', :git=> 'https://github.com/thoughtbot/paperclip', :ref => '523bd46c768226893f23889079a7aa9c73b57d68'
# aws sdk for uploading at AWS
gem 'aws-sdk', '< 2.0'
gem 's3'
在production.rb
# configuration for 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']
}
}