简体   繁体   中英

Rails Carrierwave upload not uploading

So I'm trying to get a basic carrierwave uploader working for a photography website.

I went and generated a scaffold and got the uploader all setup. the page loads showing the upload box, and it looks like I upload a photo but when I go to the show page it spits out something like Image: #<ActionDispatch::Http::UploadedFile:0x007fa03f7389a8> and shows no image.

I'm still new to rails so there is probably something really simple that im not seeing here.

Thanks!

Here is my uploader called 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 

here is my 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 %>

and this is my images controller

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

try changing your show.html.erb :

from:

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

to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM