【问题标题】:Rails 4 - Carrierwave -file uploadsRails 4 - Carrierwave 文件上传
【发布时间】:2016-04-05 00:19:53
【问题描述】:

我正在尝试在 Rails 4 中制作应用程序。

我使用 Carrierwave 进行文件上传。

我有两个模型,用户和个人资料。

他们每个人都有:

profile.rb

mount_uploader :hero, HeroUploader

用户.rb

  mount_uploader :avatar, AvatarUploader

然后我将上传者设为:

英雄:

class HeroUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  process :resize_to_fit => [950, 245]
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  version :wide do
    process :resize_to_fill => [951,245]
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

头像:

class AvatarUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def cache_dir
    "#{Rails.root}/tmp/uploads"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :resize_to_fit => [50, 50]
  # end

  process :resize_to_fit => [800, 800]
  # Create different versions of your uploaded files:
  version :thumb do
    process :resize_to_fill => [200,200]
  end

  version :profile do
    process :resize_to_fill => [345,245]
  end

  version :wide do
    process :resize_to_fill => [951,245]
  end

  version :small do
    process :resize_to_fill => [35,35]
  end


  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
     %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

我仍处于开发模式(所以不确定这是否会有所不同)。

在我的个人资料表格中,我有:

<%= simple_form_for(@profile) do |f| %>
            <%= f.error_notification %>

              <div class="form-inputs">

          <div class="intpol2">
            About you
          </div>

          <div class="row">
            <div class="col-md-4">
              <%= f.input :title,  autofocus: true %>
            </div>

            <div class="col-md-8">
              <%= f.input :occupation, :label => "Your occupation or job title" %>
            </div>

          </div>

          <div class="row">
            <div class="col-md-6">
               <%= render 'users/profileimgform', f: f %>   
            </div>

            <div class="col-md-6">
             <%= f.input :hero, as: :file, :label => "Add a background image to your profile page" %>
            </div>

          </div>

我的个人资料头像图像的用户部分表单有:

<%= simple_fields_for :user do |f| %>
  <%= f.error_notification %>

              <div class="form-inputs">


              <%= f.input :avatar, as: :file, :label => "Add a profile image (head shot)" %>

              </div>


        <% end %>

当我保存所有这些并尝试使用表单上传图片时,我得到了两个不同的结果。

对于用户头像图像,我在显示页面中没有显示任何内容。当我检查元素时,会显示一个空的 div 容器。

但是,对于个人资料英雄图像,显示页面显示了一个损坏的链接图像,当我检查元素时,我得到:

Failed to load resource: the server responded with a status of 404 (Not Found)

还有其他步骤可以完成这项工作吗?

我的配置文件控制器具有包含以下两个属性的强大参数:

def profile_params
      params[:profile].permit(:user_id, :title, :hero, :overview, :research_interest, :occupation, :external_profile, 
        :working_languages, :tag_list,
          user_attributes: [:avatar]
          )
    end

根据 Alexei 的建议,我将个人资料表格的开头行更改为:

<%= simple_form_for @profile, html: { multipart: true }  do |f| %>

当我保存并重试时,我得到与以前完全相同的错误。

在我的个人资料显示页面中,我有:

<%= image_tag '@profile.user.avatar.url.profile' if @profile.user.avatar? %>

            <%= image_tag '@profile.hero.url.wide' if @profile.hero? %> %>

这些图片标签在同一个展示页面中。头像属性在用户表中(个人资料属于用户),英雄属性在个人资料表中。错误的区别在于包含头像的 div 标签在代码检查器中只是显示为空白。包含英雄图像的 div 标签显示为断开的链接,并在代码检查器中显示 404 错误。

在我的文章显示页面中,我遇到了同样的问题,但不是渲染图像,而是显示图像文件的键入名称。

【问题讨论】:

  • 您需要在表单中添加 - multipart: true -。
  • 我在哪里写?
  • 嗨 - 我试过了(虽然我不明白为什么它是必要的,因为我正在使用 form_for)。它不起作用 - 我在上面发布了错误
  • 在 simple_form 中,你需要

标签: ruby-on-rails carrierwave


【解决方案1】:

您的多部分代码完全可以工作,只需删除 image_tag 中的引号:

<%= image_tag @profile.user.avatar.profile.url if @profile.user.avatar? %>

<%= image_tag @profile.hero.wide.url if @profile.hero? %>

【讨论】:

  • 太棒了。谢谢 - 但图像魔术调整大小不起作用未定义的方法“/uploads/profile/hero/1/croppedplane.png”:String
  • 当我在该表达式中添加 Wide 时(甚至在 url 之前),这是一个 404 错误。如果我把它拿出来,它会显示图像但不会调整它的大小
  • 文件“YOUR_APP_FOLDER/public/uploads/profile/hero/1/wide_croppedplane.png”是否存在?如果没有,重启网络服务器并再次上传图片。
  • 我是否需要做一些额外的事情才能让它在生产模式下工作。我只是尝试推送这个,我必须编辑配置文件并在生产中上传图片才能让它出现。
  • 其实——我刚刚发现这篇文章解决了这个问题github.com/carrierwaveuploader/carrierwave/wiki/…
猜你喜欢
  • 2013-11-20
  • 2016-12-22
  • 2013-02-24
  • 2015-07-05
  • 2014-02-20
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
相关资源
最近更新 更多