带载波返回的文件上传:无

FIle upload with Carrier wave returning :nil

我正在尝试将 carrierwave 添加到我的应用程序以处理来自用户的附件(pdf、doc 等 - 不是图像)。我查看了几个教程,虽然它似乎有效,但我无法通过 link_to 方法或按照 url carrierwave 创建的方法访问附件。在使用我的应用程序时,我 运行 遇到了两件奇怪的事情:

1) link 预览(指 chrome 中左下方的弹出窗口显示 link 的目的地)仅显示 localhost::model/id ,而不是上传器中的 url

2) 当我在rails 控制台查看记录时,该列显示为nil。我已将其添加到控制器中允许的参数中。

我的控制器:

  class Cms484sController < ApplicationController
  before_action :set_cms484, only: [:show, :edit, :update, :destroy]

  # GET /cms484s
  # GET /cms484s.json
  def index
    @cms484s = Cms484.all
  end

  # GET /cms484s/1
  # GET /cms484s/1.json
  def show
  end

  # GET /cms484s/new
  def new
    @cms484 = Cms484.new
  end

  # GET /cms484s/1/edit
  def edit
  end

  # POST /cms484s
  # POST /cms484s.json
  def create
    @cms484 = Cms484.new(cms484_params)

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

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

  # DELETE /cms484s/1
  # DELETE /cms484s/1.json
  def destroy
    @cms484.destroy
    respond_to do |format|
      format.html { redirect_to cms484s_url, notice: 'Cms484 was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def cms484_params
      params.require(:cms484).permit(:supplier_name, :supplier_addr, :supplier_city, :supplier_state, :supplier_zip, :frm_date, :document)
    end
end

我的上传者:

    class FileUploader < CarrierWave::Uploader::Base

      storage :file

      def store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
      end
end

我的表格:(缩写)

<%= f.file_field :file, multiple: true %>

  <div class="actions">
    <input type="submit" name="commit" value="Complete Cms484" />
  </div>
<% end %>
</body>
</html>

我的看法:

<p>
  <strong>Sup name:</strong>
  <%= @cms484.supplier_name %>
</p>
<%= link_to 'Document', @cms484.document.url %>  |
<%= link_to 'Edit', edit_cms484_path(@cms484) %> |
<%= link_to 'Back', cms484s_path %>

有什么想法吗?

Carrierwave 依赖于上传者的命名约定 class。在您的例子中,您将其命名为 FileUploader。

将允许的属性从 :document 调整为 :file

def cms484_params
  params.require(:cms484).permit(:supplier_name, :supplier_addr, :supplier_city, :supplier_state, :supplier_zip, :frm_date, :file)
end

在您看来

<%= link_to 'Document', @cms484.file_url %>