使用载波返回的FIle上传:无

时间:2015-05-23 23:44:07

标签: ruby-on-rails carrierwave attachment

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

1)链接预览(参考显示链接目的地的chrome左下角的弹出窗口)仅显示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 %>

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

Carrierwave依赖于您的上传器类的命名约定。在您的情况下,您将其命名为FileUploader。

从以下位置调整允许的属性:document to: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 %>