mysql—如何获取存储在sql数据库中的图像并将其显示在carosel中 ruby-on-rails

nfs0ujit  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(193)

我一直在想办法解决这个问题,但我觉得我遇到了麻烦。我试图从我的sql数据库中抓取第一张、第二张或第三张照片,并将其输入到我主页上的引导转盘中。这是我的照片控制器代码:

class PhotosController < ApplicationController
  before_action :set_photo, only: [:show, :edit, :update, :destroy]

  # GET /photos
  # GET /photos.json
  def index
    @photos = Photo.all
  end

  # GET /photos/1
  # GET /photos/1.json
  def show
  end
  def show_first
    @photos = Photo.find(1)
    send_data @photos.image, :type => 'image/*',:disposition => 'inline'
  end
  def show_second
    @photos = Photo.find(2)
    send_data @photos.image, :type => 'image/*',:disposition => 'inline'
  end
  def show_third
    @photos = Photo.find(3)
    send_data @photos.image, :type => 'image/*',:disposition => 'inline'
  end

  def show_image
    @photos = Photo.find(params[:id])
    send_data @photos.image, :type => 'image/*',:disposition => 'inline'
  end
  # GET /photos/new
  def new
    @photo = Photo.new
  end

  # GET /photos/1/edit
  def edit
  end

  # POST /photos
  # POST /photos.json
  def create
    @photo = Photo.new(photo_params)

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

  # PATCH/PUT /photos/1
  # PATCH/PUT /photos/1.json
  def update
    respond_to do |format|
      if @photo.update(photo_params)
        format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
        format.json { render :show, status: :ok, location: @photo }
      else
        format.html { render :edit }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /photos/1
  # DELETE /photos/1.json
  def destroy
    @photo.destroy
    respond_to do |format|
      format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_photo
      @photo = Photo.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def photo_params
      params.require(:photo).permit(:title, :medium, :description, :file)
    end
end

我一直在寻找一个好的和简单的方法来实现这一点。我尝试过使用show_first,second等方法,但我不确定我是否实现了它。有谁能解释一下,或者至少能给我指出正确的方向吗?图像/*正确吗?我基本上是做我自己的学习和任何帮助和透彻的解释将是可怕的,非常感谢!

smdncfj3

smdncfj31#

为什么不直接做呢 show 行动?


# GET photos/:id

# GET photos/:id.jpg

def show
  @photo = Photo.find(params[:id])
  respond_to do |format|
    format.html {}
    format.jpg do
      send_data @photos.image, type: 'image/jpeg', disposition: 'inline'
    end
  end
end

然后可以通过以下方式显示照片:

<% Photo.order(created_at: :desc).limit(10).each do |photo| %>
  <%= tag :img, src: path_for(photo, format: :jpg) %>
<% end %>

相关问题