Rails Error - cannot load such file -- aws-sdk (You may need to install the aws-sdk gem)
Asked Answered
A

3

5

I have a RoR app with image upload through paperclip and amazon s3. Everything was working fine until I decided to change the routes from myapp.com/id to myapp.com/model-name. Now I get the following error: LoadError cannot load such file -- aws-sdk (You may need to install the aws-sdk gem). These changes involved changing the model, the controller, and the db.

Model:

class Major < ActiveRecord::Base
  attr_accessible :glance, :name, :image

  # Validations
  validates :glance, presence: true
  validates :name, presence: true
  validates_attachment :image, content_type: {content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']}, size: { less_than: 5.megabytes }
  validates :slug, uniqueness: true, presence: true

  before_validation :generate_slug

  # Associations
  has_attached_file :image, styles: { 
   profile: "350x350>", 
   similar: '166x134>', 
   thumb: "100x100>" 
  },
    :storage => :s3,
    :bucket => 'major finder'
    :s3_credentials => {
      :access_key_id => 'my_key_id',
      :secret_access_key => 'my_secret_access_key'
    },
    :path => "/majors/:attachment/:style/:filename"    

  # make the url path memorable (instead of using the id)
  def to_param
    slug
  end

  def generate_slug
    self.slug ||= name.parameterize
  end 
end

Controller:

class MajorsController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :edit]
  before_filter :find_page, only: [:show, :edit, :update, :destroy]

  def index
    @majors_recent = Major.order("created_at DESC")
    @majors = Major.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @majors }
    end
  end

  def show
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @major }
    end
  end

  def new
    @major = Major.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @major }
    end
  end

  def edit
  end

  def create
    @major = Major.new(params[:major])

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

  def update
    respond_to do |format|
      if @major.update_attributes(params[:major])
        format.html { redirect_to @major, notice: 'Major was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @major.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @major.destroy

    respond_to do |format|
      format.html { redirect_to majors_url }
      format.json { head :no_content }
    end
  end

private 
  def find_page
    @major = Major.find_by_slug!(params[:id])
  end
end

My database schema looks like this:

  create_table "majors", :force => true do |t|
    t.text     "glance",             :limit => 255
    t.datetime "created_at",                        :null => false
    t.datetime "updated_at",                        :null => false
    t.string   "name"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

Here's my Gemfile:

gem 'rails', '3.2.11'
gem 'jquery-rails'
gem 'devise'
gem 'simple_form'
gem 'aws-sdk'
gem "paperclip", "~> 3.0"
gem 'sunspot_rails'
gem 'activeadmin'


group :production do
  gem 'pg'
end

group :development, :test do
  gem 'sqlite3'
  gem 'sunspot_solr'
end

group :assets do
  gem 'sass-rails', '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
  gem 'bootstrap-sass', '~> 2.2.2.0'
end

I ran $ rails g migration add_slug_to_majors slug:index and then $ rake db:migrate Now even though I've removed all the changes I am still getting this error. I have looked all over and can't find a solution. Does anyone have any ideas?

Alic answered 25/2, 2013 at 23:21 Comment(1)
If your Gemfile includes aws-sdk-s3 and you're getting this error, it means you need to upgrade your paperclip gem version to 6+, when they started supporting the aws-sdk-s3 gem.Jujitsu
B
3

The solution is written in error, you need aws-sdk gem also in development mode (as you're using it in your model). Simply move gem 'aws-sdk' outside the group.

BTW: Use the same DB engine in dev and production mode, I have some problems with thet configuration as you have.

Bekelja answered 25/2, 2013 at 23:33 Comment(4)
That makes sense! Thank you! I can't believe I missed that. Thank you!!Alic
What problems do you have with the configuration? Thanks for all your help!Alic
Postgres parse arguments passed to DB in other way than SQLite so in dev all tests pass but production doesn't work.Bekelja
the solution is not that obvious see my answer below. Since many items in a larger project are written by other developers the problem I was experiencing required a downgrade into a specific gemThelmathem
T
9

This is actually an issue you might experience with managing versions within the Gemfile.

In my case it a newer version of paperclip causing the issue. Downgrading to and older version fixed it for me.

Thelmathem answered 18/12, 2017 at 19:27 Comment(2)
Happened to me too with version 6.0.0Petrel
I can confirm that problem with paperclip too (manually added as an upgrade for mongoid-paperclip (unmaintained since 4 years ago)), solved only on version 6.0.0.Woollyheaded
B
3

The solution is written in error, you need aws-sdk gem also in development mode (as you're using it in your model). Simply move gem 'aws-sdk' outside the group.

BTW: Use the same DB engine in dev and production mode, I have some problems with thet configuration as you have.

Bekelja answered 25/2, 2013 at 23:33 Comment(4)
That makes sense! Thank you! I can't believe I missed that. Thank you!!Alic
What problems do you have with the configuration? Thanks for all your help!Alic
Postgres parse arguments passed to DB in other way than SQLite so in dev all tests pass but production doesn't work.Bekelja
the solution is not that obvious see my answer below. Since many items in a larger project are written by other developers the problem I was experiencing required a downgrade into a specific gemThelmathem
I
1

I ran into this issue today (10/5/2021) re-writing an app called FilterTRAK from Rails v4.2.1 to Rails v6.1.4.1.

I was getting the exact error in the subject of this question.

I was unable to downgrade paperclip due to a dependency issue on Mimemagic 0.3.0 which has been YANKED.
Mimemagic 0.3.7 is all that is available and to do that you have to go up to paperclip 5.0; problem is paperclip 5.0 gave the same error.

SOLUTION:
The gem paperclip-aws I installed that gem specifically like this:

gem 'paperclip-aws', '~> 1.6', '>= 1.6.8'

And my application loaded up correctly.

Ireful answered 5/10, 2021 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.