使用mount_uploader Rails未初始化的常量错误

时间:2017-08-03 05:22:05

标签: ruby-on-rails carrierwave scaffolding

我正在使用 Rails脚手架创建一个名为 Movies 的简单模型,其中包含电影的namedirector,{ {1}}和synopsis

我正在使用 Carrierwave gem来上传海报图片。当我第一次启动rails服务器时,收到以下消息:

  

MoviesController中的NameError#new

     

未初始化的常量Movie :: PosterUploader

错误屏幕显示的提取源是我的poster文件:

models/movie.rb

这是我的电影控制器:

class Movie < ApplicationRecord
  mount_uploader :poster, PosterUploader
end

当我使用Rails脚手架创建模型时,我将海报制作成了一个字符串,但是将其更改为在此部分的电影中形成部分文件:

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

这是我的<div class="field"> <%= form.label :poster %> <%= form.file_field :poster, id: :movie_poster %> </div> 文件,以防我在那里发生错误:

routes

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

  

未初始化的常量Movie :: PosterUploader

您应该生成 上传者。做

rails generate uploader Poster

应生成文件

app/uploaders/poster_uploader.rb