【问题标题】:Excon::Errors::SocketError in PostsController#createPostsController#create 中的 Excon::Errors::SocketError
【发布时间】:2016-06-01 16:11:06
【问题描述】:

我正在使用 Rails 建立一个照片博客,但遇到了这个错误:Excon::Errors::SocketError in PostsController#create

对等方重置连接 (Errno::ECONNRESET)

提取的源代码(第 30 行附近):

respond_to do |format|
  if @post.save
    format.html { redirect_to @post, notice: 'Post was successfully created.' }
    format.json { render :show, status: :created, location: @post }
  else


(0.2ms)  BEGIN
  SQL (0.3ms)  INSERT INTO "posts" ("title", "image", "description", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["title", "Title test"], ["image", "mcm-magnavox-stereo-1.jpg"], ["description", "Description test"], ["created_at", "2016-02-19 22:13:34.023935"], ["updated_at", "2016-02-19 22:13:34.023935"]]
   (0.9ms)  ROLLBACK
Completed 500 Internal Server Error in 6027ms (ActiveRecord: 1.6ms)

Excon::Errors::SocketError (Connection reset by peer (Errno::ECONNRESET)):
  app/controllers/posts_controller.rb:30:in `block in create'
  app/controllers/posts_controller.rb:29:in `create'

这是我的帖子模型:

class Post < ActiveRecord::Base
    validates_presence_of :title, :image

    mount_uploader :image, PhotoUploader
end

这是我的后期控制器:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :image, :description)
    end
end

上传者

# encoding: utf-8

class PhotoUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:   # include CarrierWave::RMagick   include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:   # include Sprockets::Helpers::RailsHelper   # include Sprockets::Helpers::IsolatedHelper

  # Include the sprockets-rails helper for Rails 4+ asset pipeline compatibility:   include Sprockets::Rails::Helper

  # Choose what kind of storage to use for this uploader:   # storage :file   storage :fog

  # Override the directory where uploaded files will be stored.   # This is a sensible default for uploaders that are meant to be mounted: def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"   end

  # Provide a default URL as a default if there hasn't been a file uploaded:   # def default_url   #   # For Rails 3.1+ asset pipeline compatibility:   #   # asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))   #   #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')   # end

  # Process files as they are uploaded:   #process :resize_to_fill => [200, 200]   #   # def scale(width, height)   #   # do something   # end

  # Create different versions of your uploaded files:   version :tiny do
    process :resize_to_fill => [20, 20]   end

  version :profile_size do
    process :resize_to_fill => [300, 300]   end

  # version :full_size do   #   process :resize_to_fill => [700, 700] 
# end

  # Add a white list of extensions which are allowed to be uploaded.  
# For images you might use something like this:   def extension_white_list
    %w(jpg jpeg gif png)   end

  # Override the filename of the uploaded files:   # Avoid using model.id or version_name here, see uploader/store.rb for details.   # def filename   #   "something.jpg" if original_filename   # end

end

载波配置(fog.rb)

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => "AWS",
    :aws_access_key_id      => ENV['AWS_ACCESS_KEY_ID'],
    :aws_secret_access_key  => ENV['AWS_SECRET_ACCESS_KEY']
  }

  config.fog_directory  = ENV['AWS_BUCKET']
  config.fog_public     = false
  config.fog_region     = ENV['AWS_REGION']
end

【问题讨论】:

  • 问题似乎来自您的 Post 模型(尤其是某些能力的 save 方法)。您能否分享相关代码以查看是否有助于缩小范围?谢谢!
  • 感谢您迄今为止的帮助,我已经用更多信息更新了我的问题。
  • 谢谢,希望越来越近。那你用的是 Carrierwave 吗?从对 PhotoUploader 的模型参考中看起来有点像?很抱歉继续往下走这个看似兔子洞,但罪魁祸首可能在那个方向。如果它只是使用非常标准的 Carrierwave,它可能不是这样,但仔细检查您的配置是什么样子可能会很好(但请确保不要发布任何凭据,很容易意外)。
  • 我正在使用 Carrierwave,它看起来不错,但我会用上传者信息更新帖子。
  • 是的,看起来没什么异常。我一定会仔细检查所有 ENV 内容是否具有您期望的值。如果区域或存储桶为 nil 或空字符串,它可能会执行所描述的行为,但我希望密钥/秘密丢失会有所不同。

标签: excon


【解决方案1】:

我的地区没有价值。我首先设置 us-west-1 并返回错误,但将区域更改为 us-east-1 有效。 谢谢吉姆斯!

【讨论】:

  • 很高兴能帮上忙!
猜你喜欢
  • 2013-07-20
  • 1970-01-01
  • 2012-12-14
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多