ruby-on-rails-4 - 在 ruby​​ on rails 4 中上传图像或文件

标签 ruby-on-rails-4

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

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

screenshot of show page

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

这是我的 ImagesController.rb 文件:

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 文件:

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

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

这是我的 schema.rb 文件:

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 的情况下显示它们。

最佳答案

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

此外,您可能还想阅读有关 Assets 管道的信息以及它们为何不显示:

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

关于ruby-on-rails-4 - 在 ruby​​ on rails 4 中上传图像或文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25364093/

相关文章:

ruby-on-rails - Minimagick:未定义方法 `destroy!' 为 true:TrueClass auto_orient

ruby-on-rails - ActionController::ParameterMissing in UsersController#show

css - 从桌面更改为移动时如何调整(裁剪)图像

postgresql - 如何在 Rails 控制台中查找数据库名称

ruby-on-rails - 部署到 Heroku(表不存在错误)

css - Rails 4 不是 image_url 的正确路径

ruby-on-rails - 如何为门卫的 skip_authorization block 指定 super 应用列表?

ruby-on-rails - 加密/自动过期数据库中的敏感信息

mysql - 将新的 Rails 应用程序连接到现有的 MySQL 数据库

ruby-on-rails - Ruby 2.1.1,找不到 Railties