【发布时间】:2015-11-29 13:24:13
【问题描述】:
我最近将回形针 gem 添加到我的网络应用程序中。然后我添加了 aws-sdk 以便我可以在我的应用程序的生产版本上上传图像,该版本位于 heroku 上。在配置完所有内容并尝试上传图像后,我得到了标准的 rails 错误页面,并进入了日志:
NameError (uninitialized constant Paperclip::Storage::S3::AWS):
app/controllers/pins_controller.rb:23:in `create'
我在网上搜索,发现这是 aws-sdk 版本 2 的问题,我不得不降级 gem。所以我将 gem 降级为使用版本 1,然后运行 bundle install,用 git 提交我的更改,并将其推送到 heroku。
这一次,当我尝试将照片附加到图钉时,我遇到了另一个错误。这让我相信之前的错误已修复,而我现在遇到了不同的问题。这是错误:
NoMethodError (undefined method `first' for nil:NilClass):
app/controllers/pins_controller.rb:23:in `create'
我进行了大量研究,但无法找出问题所在。我是 Rails 的初学者,我正在通过制作一个类似于 pinterest 的网络应用程序来学习它,我可以在其中允许用户创建帐户和发布 pin,我正在使用设计来做到这一点。我添加了回形针以便我可以启用图像发布,并添加了 aws-sdk 以便我可以使用 amazon S3 存储照片,因为我的生产环境在 heroku 上,而 heroku 无法存储图像。我将粘贴一些更有用的代码:
Controller(app/controllers/pins_controller.rb):这是错误日志中提到的控制器文件。错误在第 23 行,就像它在日志上所说的那样。第 23 行是“创建”操作中的一行:if @pin.save
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all
end
def show
end
def new
@pin = current_user.pins.build
end
def edit
end
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully added.'
else
render :new
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully edited.'
else
render :edit
end
end
def destroy
@pin.destroy
redirect_to pins_url, notice: 'Pin was successfully deleted.'
end
private
def set_pin
@pin = Pin.find(params[:id])
end
def correct_user
@pin = current_user.pins.find_by(id: params[:id])
redirect_to pins_path, notice: "You are not permitted to edit this pin" if @pin.nil?
end
def pin_params
params.require(:pin).permit(:description, :image)
end
end
模型(app/models/pin.rb):这是引脚的模型(基本上是帖子)
class Pin < ActiveRecord::Base
belongs_to :user
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
配置(config/environments/production.rb):这是我添加aws配置变量的生产文件
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like
# NGINX, varnish or squid.
# config.action_dispatch.rack_cache = true
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# Use the lowest log level to ensure availability of diagnostic information
# when problems arise.
config.log_level = :debug
# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = 'http://assets.example.com'
# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
end
表单(app/views/pins/_form.html.erb):这是用来提交pins的表单
<%= form_for @pin, html: { multipart: true } do |f| %>
<% if @pin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@pin.errors.count, "error") %> prohibited this pin from being saved:</h2>
<ul>
<% @pin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :image %><br>
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %><br>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
迁移(db/migrate/20150902154233_add_attachment_image_to_pins.rb):这是我所做的迁移,以便我可以将照片附加到我的图钉上
class AddAttachmentImageToPins < ActiveRecord::Migration
def self.up
change_table :pins do |t|
t.attachment :image
end
end
def self.down
remove_attachment :pins, :image
end
end
Gemfile:这是我的 gemfile,我使用的是 rails4、回形针 4.3 和 aws-sdk 1.6.5
source 'https://rubygems.org'
ruby '2.2.1'
gem 'rails', '4.2.4'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bootstrap-sass', '~> 3.3.5'
gem 'devise', '~> 3.5.2'
gem "paperclip", "~> 4.3"
gem 'aws-sdk', '< 2.0'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
我现在真的被困住了,我不知道该怎么办。我希望有人能帮帮忙。如果有什么我忘记补充的,请告诉我,我会补充的。
【问题讨论】:
-
您没有显示用户模型。用户有很多 Pin 图吗?我会亲自尝试在 Rails 控制台中构建/创建,并仔细检查我的日志以获取其他线索,例如未经许可的属性。另外,我认为
gem 'aws-sdk-v1'可能更安全,尽管我同意您可能已经解决了这个问题。 -
你好,谢谢你的帮助。经过几个小时的真正生气后,它又开始工作了。这一定是因为我尝试过的一件事。我能够创建一个带有图像的图钉,但起初它显示的是一个空图像。然后我知道它与S3有关,所以我想出了如何修复它。我制作了一个符合美国标准的新桶,图像开始显示。无论如何谢谢。= :)
-
很高兴听到它对您有用
标签: ruby-on-rails ruby-on-rails-4 heroku amazon-s3 paperclip