2

I'm setting up a photo blog with Rails and am having difficulty with this error: Excon::Errors::SocketError in PostsController#create

Connection reset by peer (Errno::ECONNRESET)

Extracted source (around line #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'

Here's my post model:

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

    mount_uploader :image, PhotoUploader
end

Here's my post controller:

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

Uploader

# 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

Carrierwave config (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
TS11
  • 75
  • 9
  • It seems likely that the issue is coming from within your Post model (the save method in some capacity, in particular). Could you share the related code to see if that helps narrow things down? Thanks! – geemus Feb 22 '16 at 17:25
  • Thank you for your help thus far, I've updated my question with more information. – TS11 Feb 22 '16 at 19:52
  • Thanks, hopefully getting closer. Are you using Carrierwave then? Kind of looks like it from the model reference to PhotoUploader? Sorry to keep going down this seeming rabbit hole, but the culprit may be in that direction. If it is just using pretty standard Carrierwave it probably isn't that, but it might be good to double check what your configuration looks like (be sure not to post any credentials though, easy to do accidentally). – geemus Feb 23 '16 at 15:00
  • I am using Carrierwave, it looks fine but I'll update the post with the uploader info. – TS11 Feb 23 '16 at 17:17
  • Yeah, doesn't look unusual or anything. I'd be sure to double-check that all the ENV stuff has the values you expect. It might do the described behavior if the region or bucket were nil or empty string, but I would expect something different for the key/secret missing. – geemus Feb 23 '16 at 20:39
  • You were correct, it was an absent region value. Now i have a new error. :D – TS11 Mar 05 '16 at 06:53

1 Answers1

1

I didn't have a value for my region. I set us-west-1 first and that returned an error but changing the region to us-east-1 worked. Thank you Geemus!

TS11
  • 75
  • 9