回形针 has_attached_file 不适用于 Rails 建立连接 [英] Paperclip has_attached_file not working with Rails establish_connection

查看:74
本文介绍了回形针 has_attached_file 不适用于 Rails 建立连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 created_connection 从回形针访问图像.
这是我的模型代码 article.rb

类文章

文件 asset.rb

class Asset {:大=>"1200x700",:medium =>800 倍"}validates_attachment_content_type :image, :content_type =>[图像/jpg"、图像/png"、图像/jpeg"]结尾

查看代码

<% 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%>"/><%结束%><div class="hover-title"><h2 class="project-title"><%= article.name%></h2><p><%=property.short_desc %></p>

<%结束%>

它会抛出这样的错误

<块引用>

未定义的Asset方法`has_attached_file'(调用'Asset.connection'建立连接):Class

解决方案

您需要在现有项目中安装 paperclip gem,从您需要访问的项目中复制并配置 AWS S3 信息.
在 Gemfile 中

# 用于图像处理的回形针 gem宝石'回形针',:git =>'https://github.com/thoughtbot/paperclip', :ref =>'523bd46c768226893f23889079a7aa9c73b57d68'# aws sdk 用于在 AWS 上传gem 'aws-sdk', '<2.0'宝石's3'

在生产.rb

# 亚马逊 s3 的配置config.paperclip_defaults = {:存储=>: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']}}

I am trying to access images from paperclip using establish_connection.
Here is my 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 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 %>

it throws error like this

undefined method `has_attached_file' for Asset (call 'Asset.connection' to establish a connection):Class

解决方案

You need to install paperclip gem in the existing project, copy and configure AWS S3 information from the project which you need to access.
In 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'

In 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']
    }
  }

这篇关于回形针 has_attached_file 不适用于 Rails 建立连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆