Rails Carrierwave上传不上传

时间:2015-06-18 07:25:06

标签: ruby-on-rails ruby file-upload carrierwave

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

我去了并制作了一个脚手架并且上传了所有设置。页面加载显示上传框,看起来我上传了一张照片,但是当我转到展示页面时,它会显示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

1 个答案:

答案 0 :(得分:0)

尝试更改show.html.erb

从:

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

为:

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