Rails 载波上传不上传

Rails Carrierwave upload not uploading

所以我试图让一个基本的载波上传器为一个摄影网站工作。

我去生成了一个脚手架,并完成了上传器的所有设置。页面加载显示上传框,看起来我上传了一张照片但是当我进入显示页面时它吐出类似 Image: #<ActionDispatch::Http::UploadedFile:0x007fa03f7389a8> 的内容并且没有显示图像。

我对 rails 还是个新手,所以可能有些非常简单的东西我在这里没有看到。

谢谢!

这是我的上传者 image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
  version :thumb do
  process :resize_to_limit => [200, 200]
  end
end 

这是我的 show.html.rb

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @image.title %>
</p>

 <p>
     <strong>Description:</strong>
    <%= @image.description %>
</p>

<p>
  <strong>Tag:</strong>
  <%= @image.tag %>
</p>

<p>
  <strong>Image:</strong>
  <%= @image.image %>
</p>

<%= link_to 'Edit', edit_image_path(@image) %> |
<%= link_to 'Back', images_path %>

这是我的图像控制器

class ImagesController < ApplicationController
  before_action :set_image, only: [:show, :edit, :update, :destroy]

  # GET /images
  # GET /images.json
  def index
    @images = Image.all
  end

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

  # GET /images/new
  def new
    @image = Image.new
  end

  # GET /images/1/edit
  def edit
  end

  # POST /images
  # POST /images.json
  def create
    @image = Image.new(image_params)

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

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

  # DELETE /images/1
  # DELETE /images/1.json
  def destroy
    @image.destroy
    respond_to do |format|
      format.html { redirect_to images_url, notice: 'Image was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def image_params
      params.require(:image).permit(:title, :description, :tag, :image)
    end
end

尝试更改您的 show.html.erb:

来自:

<p>
  <strong>Image:</strong>
  <%= @image.image %>
</p>

至:

<p>
  <strong>Image:</strong>
  <%= image_tag @image.image %>
</p>