简体   繁体   English

Rails Carrierwave上传未上传

[英]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. 该页面加载后显示上载框,看起来就像我上载一张照片,但是当我进入显示页面时,它会吐出类似Image: #<ActionDispatch::Http::UploadedFile:0x007fa03f7389a8>显示任何图像。

I'm still new to rails so there is probably something really simple that im not seeing here. 我仍然对Rails还是陌生的,所以可能有些非常简单的东西在即时通讯中看不到。

Thanks! 谢谢!

Here is my uploader called image_uploader.rb 这是我的上传器,名为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 这是我的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 : 尝试更改您的show.html.erb

from: 从:

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

to: 至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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