在 ruby​​ on rails 4 中上传图像或文件 [英] uploading image or file in ruby on rails 4

查看:33
本文介绍了在 ruby​​ on rails 4 中上传图像或文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我想要一个关于在不使用任何 gemfile 的情况下上传文件或图像的清晰概念(例如:peperclip、carrierwave 等).

Here, I want a clear concept about uploading file or image without using any gemfile(Ex: peperclip, carrierwave etc).

以前,我做过一些工作.我可以在哪里上传/assets/images"文件夹中的图片.但是当我在我的显示页面中调用它时我看不到图像(只有一个中断图像的占位符).这是显示页面的屏幕截图:

Previously, I did some work. Where i can upload images in "/assets/images" folder. But i could't see images when i call it in my show page(there was only a placeholder of break image). here is the screenshot of show page:

如何在我的 show.html.erb 文件中显示这些图像?

how can i show those images in my show.html.erb file?

这是我的 ImagesController.rb 文件:

Here is my ImagesController.rb file:

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

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

  # GET /images/1
  def show
  end

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

  # GET /images/1/edit
  def edit
  end

  # POST /images
  def create
    @image = Image.new(image_params)
    if params[:image].present?
      file = params[:image][:picture]
      File.open(Rails.root.join('app','assets', 'images', file.original_filename), 'wb') do |f|
        f.write(file.read)
      end
    end

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

    end
  end


  def update
    respond_to do |format|
      if @image.update(image_params)
        format.html { redirect_to @image, notice: 'Image was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @image.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /images/1
  def destroy
    @image.destroy
    respond_to do |format|
      format.html { redirect_to images_url }
      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(:name)
    end
end

这是我的 show.html.erb 文件:

Here is my show.html.erb file:

<p><%= @image.name %></p>

<p><%= image_tag @image.picture %></p>

这是我的 schema.rb 文件:

Here is my schema.rb file:

ActiveRecord::Schema.define(version: 20140725043842) do

  create_table "images", force: true do |t|
    t.string   "name"
    t.string   "picture"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

如果您需要有关我的项目的更多信息,请告诉我.请给我一些建议.我想清楚地了解上传图像/文件并在不使用 gemfile 的情况下显示它们.

please tell me, if you need more information about my project. please give me some advice. I want a clear understand about uploading image/file and show them without using gemfile.

推荐答案

您必须像资产一样将图像上传到不需要预编译才能显示的文件夹.尝试将它们上传到 public 文件夹.

You have to upload images to a folder that doesn't need precompiling to be show, as assets do. Try uploading them to public folder.

此外,您可能还想了解资产管道以及它们为什么不显示:

Also you might want to read about the asset pipeline and why aren't they displaying:

http://guides.rubyonrails.org/asset_pipeline.html

这篇关于在 ruby​​ on rails 4 中上传图像或文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆