如何使用Rails中的单个上传图像来简化图像上传

时间:2018-12-23 14:23:28

标签: ruby-on-rails

我想将许多图像上传到作品集和单个图像的图片

我指的是这个carrierwave gem in github

video_new.html

<%= form_for :video, html: { multipart: true }, url: video_create_path do |f| %>
 <div class="field">
    <%= f.label :locallage %>
    <%= f.file_field :locallage %>

    <%= f.label :content %>
    <%= f.text_area :content %>

    <%= f.label :price %>
    <%= f.text_field :price %>

    <%= f.label :portfolio %><br>
    <%= f.file_field :portfolio, multiple: true %>

    <%= f.submit %>

<% end %>

控制器

class FreelancersController < ApplicationController
  def index
  end

  def new
  end

  def category
    @category = Category.new(category_new)

    if (@category.category == "영상")
      redirect_to video_new_path
    elsif (@category.category == "디자인")
      redirect_to design_path
    end

  end

  def video
    @videos = Video.all
  end

  def video_new
  end

  def video_create
    @video = Video.new(video_params)
    @video.save

    render "freelancers/video_show"
  end

  def video_show
    @video = Video.find(params[:id])
  end

  def design
  end

  def design_new
  end

  def design_create
  end


  private
    def category_new
      params.require(:category).permit(:category)
    end

    def video_params
      params.require(:video).permit(:content, :price, :locallage, {portfolio: []})
    end

end

video.rb

class Video < ApplicationRecord
  mount_uploader :locallage, LocallageUploader
  mount_uploader :portfolio, PortfolioUploader
  serialize :portfolio, JSON
end

locallage_uploader.rb

class LocallageUploader < CarrierWave::Uploader::Base
  storage :fog

  def store_dir
    "uploads/locallage"
  end
end

portfolio_uploader.rb

class PortfolioUploader < CarrierWave::Uploader::Base
  storage :fog

  def store_dir
    "uploads/locallage"
  end
end

video_show.html

<%= @video.content %>
<%= @video.price %>
<img src="<%= @video.locallage.url %>">

<img src="<%= @video.portfolio.url %>">

当我使用 locallage_uploader 时,结果是正确的,但是当我使用 portfolio_uploader 时,文件未正确保存。

  

以这种方式使用Portfolio_uploader时,我没有得到结果。
@ video.portfolio 1。url   或
@ video.portfolio [2] .url

0 个答案:

没有答案