如何在我的模型中正确创建这个Ruby方法?

时间:2014-01-16 18:20:04

标签: ruby-on-rails ruby

当我提交表单时,它会附带一个附件。我正在尝试创建一个方法,找出附件是图像还是视频,然后保存它,因为有一个视频列和一个图像列。

以下是我的模型中的代码:

class Medium < ActiveRecord::Base
  belongs_to :mediumable, :polymorphic => true

  has_attached_file :image, :url => "/system/:class/:image/:id/:style/:filename", :styles => { :thumbnail => '174X174>', :large => '470' }

  attr_accessible :attachment

  validates :attachment, :presence => true

  def attachment
    debugger
    image || video
  end

  def attachment=(value)
    value.content_type.include? "image" ? image = value : video = value
  end
end

这是我尝试提交任何内容时遇到的错误:

TypeError (can't convert ActionDispatch::Http::UploadedFile into String):
  app/models/medium.rb:17:in `include?'
  app/models/medium.rb:17:in `attachment='
  app/controllers/media_controller.rb:17:in `create'

0 个答案:

没有答案