Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 818
lflores Avatar asked Feb 25 '13 23:02

lflores


2 Answers

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.

like image 156
copremesis Avatar answered Oct 08 '22 12:10

copremesis


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.

like image 31
Hauleth Avatar answered Oct 08 '22 10:10

Hauleth