Rails 4 - Carrierwave -file上传

时间:2015-12-31 00:40:20

标签: ruby-on-rails carrierwave

我试图在Rails 4中创建一个应用程序。

我使用Carrierwave上传文件。

我有两个型号,用户&轮廓。

他们每人都有:

profile.rb

mount_uploader :hero, HeroUploader

user.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

根据阿列克谢的建议,我将个人资料表格的开头行改为:

<%= 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? %> %>

这些图片代码位于同一个展示页面中。 avatar属性位于users表中(配置文件属于用户),hero属性位于配置文件表中。错误的不同之处在于包含头像的div标签在代码检查器中显示为空白。包含英雄图像的div标签显示为断开的链接,并在代码检查器中显示404错误。

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

1 个答案:

答案 0 :(得分:1)

您的代码与多部分完全一致,只需删除image_tag中的引号:

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

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