Paperclip抛出"未定义的方法' gsub' for File"

时间:2017-07-27 04:42:37

标签: ruby-on-rails ruby paperclip gsub

对于我的应用程序,我开始使用paperclip gem。几乎一切都很好。当我填满我的桌子时#34;用户"我跟着https://github.com/thoughtbot/paperclip#quick-start这个链接的图像。 之后我为我的db创建了一个名为" addpictures"我在哪里用的东西:

task addpicture: :environment do
  User.all.each do |user|
    user.update image: File.open(Dir.glob(File.join(Rails.root, "app", "assets", "images", "svg", "*")).sample)
  end
end

我运行之后使用rails db:addpicture 1到1001用户可以通过User.first.image.url获取我可以访问的图片,但是其中一些从1002到1019没有图片。为什么?你能解释一下吗?

第二件事是我尝试为附件设置默认图片,如:

has_attached_file :image, :default_url => File.open(Dir.glob(File.join(Rails.root, "app", "assets", "images", "original", "*")).sample)

但它不起作用,当我尝试通过User.last.image.url获取此图片时,我收到错误"没有方法gsub for File"。我尝试在互联网上找到解决方案,但没有任何帮助。

更新:

查看:

<div id="welcome-board" class="welcome-board">
 <aside id="left-side-menu" class="left-side-menu">
  <%= render 'partials/side_menu' %>
 </aside>
<div id="dashboard" class="dashboard">
<input type="search" placeholder="Find mum" id="find-mum-search" class="find-mum-search">
<ul id="mums-list" class="mums-list">
<% @mums.each do |mum| %>
  <li>
  <%= image_tag mum.image.url, id: "profile", class: "profile" %>
   <div id="profile" class="profile">
     <h3><%= "#{truncate mum.name, :length => 16}" %></h3>
     <%= link_to "Profile |", show_profile_path(mum.id), id: "view-profile", class: "view-profile" %>
     <%= link_to "Comment", show_profile_path(mum.id) + "#comments", id: "comment-profile", class: "comment-profile" %>
     <p><%= mum.status.to_s.capitalize %></p>
   </div>
  </li>
<% end %>
</ul>
</div>
</div>

型号:

class User < ApplicationRecord
 enum status: [:coffee, :shopping, :helpful]

 has_many :comments

 has_one :address

 accepts_nested_attributes_for :address

 has_attached_file :image, :default_url => '/images/missing.png'
 validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/

 validates :name, presence: true, length: { minimum: 3 }
 validates_associated :address
 validates :password, confirmation: true, length: { in: 6..20 }
 validates :email, presence: true, format: { with: /\A.+@.+\z/}, uniqueness: true
end

表:

create_table "users", force: :cascade do |t|
 t.string "name"
 t.string "email"
 t.string "password"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.integer "status"
 t.string "image_file_name"
 t.string "image_content_type"
 t.integer "image_file_size"
 t.datetime "image_updated_at"
 t.index ["id"], name: "index_users_on_id"
end

问题是为什么我的浏览器没有显示图像?即使文件存在于model.image.url指向的文件夹中。

1 个答案:

答案 0 :(得分:0)

当您在Ruby中打开File时,需要在完成后关闭它。您可以通过调用#close对象上的File来执行此操作,或者如果将块传递给File.open,它将在块结束时自动关闭。我建议你使用块语法并像这样重写你的任务:

task addpicture: :environment do
  User.all.each do |user|
    File.open(Dir.glob(Rails.root.join('app/assets/images/svg/*').to_s).sample) do |file| 
      user.update image: file
    end
  end
end

这可能实际上解决了您的第一个问题。您有1000多个打开的文件,并且可以达到可用文件描述符的限制。

对于第二个问题,:default_url的{​​{1}}选项需要has_attached_file个参数,但String的返回值是File.open个对象。当您致电FileUser.last.image.url为您定义了paperclip方法,尝试按您提供的默认值调用url,因此错误gsub

no method gsub for File应该是应用于每条记录的一个通用默认值,但您似乎正在尝试从default_url目录中获取实际文件的样本。以下是宝石快速入门指南中的示例:

assets

按照此示例,如果您将默认图片放在class User < ActiveRecord::Base has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png" end 并设置./public/images/missing.png,则该图片应该有效。