Ben Anderson
Ben Anderson

Reputation: 19

Rails uninitialized constant error with mount_uploader

I am using Rails scaffold to create a simple model called Movies that contains the movie's name, director, synopsis and poster.

I am using the Carrierwave gem to upload the poster image. When I first boot up the rails server, I get the following message:

NameError in MoviesController#new

uninitialized constant Movie::PosterUploader

The extracted source the error screen displays is my models/movie.rb file:

class Movie < ApplicationRecord
  mount_uploader :poster, PosterUploader
end

Here is my movies controller:

class MoviesController < ApplicationController
  before_action :set_movie, only: [:show, :edit, :update, :destroy]

  # GET /movies
  # GET /movies.json
  def index
    @movies = Movie.all
 end

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

   # GET /movies/new
  def new
    @movie = Movie.new
  end

  # GET /movies/1/edit
  def edit
  end

  # POST /movies
  # POST /movies.json
  def create
    @movie = Movie.new(movie_params)

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

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

  # DELETE /movies/1
  # DELETE /movies/1.json
  def destroy
    @movie.destroy
    respond_to do |format|
      format.html { redirect_to movies_url, notice: 'Movie was successfully 
destroyed.' }
       format.json { head :no_content }
    end
   end

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

    # Never trust parameters from the scary internet, only allow the white 
list through.
    def movie_params
      params.require(:movie).permit(:title, :director, :synopsis, :poster)
    end
end

When I created the model using Rails scaffold I made the poster a string, but changed that to file in this section of Movies form partial:

<div class="field">
    <%= form.label :poster %>
    <%= form.file_field :poster, id: :movie_poster %>
  </div>

Here is my routes file just in case I have made an error there:

Rails.application.routes.draw do
  resources :movies
  root 'movies#index'
  # For details on the DSL available within this file, see 
http://guides.rubyonrails.org/routing.html
end

Any help would be greatly appreciated.

Upvotes: 0

Views: 937

Answers (1)

Pavan
Pavan

Reputation: 33542

uninitialized constant Movie::PosterUploader

You should generate the uploader. Do

rails generate uploader Poster

which should generate the file

app/uploaders/poster_uploader.rb

Upvotes: 1

Related Questions