【问题标题】:Paperclip undefined method `title_file_name' error in Rails 4.0.1Rails 4.0.1 中的回形针未定义方法“title_file_name”错误
【发布时间】:2013-12-17 23:55:14
【问题描述】:

我按照回形针 github 页面上的手册安装了它,但我得到了给定的错误。我做错了什么?

我有 4 个输入字段:标题 (text_field)、描述 (text_area)、价格 (text_field) 和图片 (file_field)。为什么我什至会收到带有前缀 title 的错误? title 字段与它有什么关系,可能有任何冲突吗?我确实创建并运行了迁移,所以我觉得这真的很奇怪。

任何帮助表示赞赏。谢谢。

编辑:

迁移如下:

class AddImageColumnsToProducts < ActiveRecord::Migration
  def change
    add_attachment :products, :image
  end
end

结果如下:

image_file_name varchar(255)
image_content_type varchar(255) 
image_file_size int(11)
image_updated_at datetime

型号:

class Product < ActiveRecord::Base
    has_attached_file   :image, :styles => { :medium => "600x600>", :thumb => "258x258>" }, 
                        :default_url => "images/:style/:slug.png"
    validates :title, :content, :image, :attachment_presence => true
    validates_with AttachmentPresenceValidator, :attributes => :image
end

控制器:

  def create
    @product = Product.new(product_params)
    @product.image = params[:product][:image]

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render action: 'show', status: :created, location: @product }
      else
        format.html { render action: 'new' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

【问题讨论】:

  • 您能否显示包含图像的表格的参数和表格列?
  • 能否请您也粘贴确切的错误。
  • 感谢您的错误。您也可以发布型号代码吗?我想知道你是否在你的模型中正确使用了 PaperClip
  • 另外,发布您的控制器代码。你做了一个简单的@product = Product.new(params[:product]) 还是一些花哨的东西?如果您更新了问题,请在此处发表评论。这是您修改后我收到通知的唯一方式。
  • @SatyaKalluri 更新了它:)

标签: ruby-on-rails file-upload ruby-on-rails-4 paperclip


【解决方案1】:

问题在于您的验证。写着

的那一行
validates :title, :content, :image, :attachment_presence => true 

假定标题、内容和图像为 3 个基于图像的属性。但是,我知道只有“图像”是基于图像的字段。所以,你的代码应该是:

validates :title, :content, :presence=>true
validates :image, :attachment_presence => true 

另外,我在请求日志中看不到“内容”字段。我想,你的意思是“描述”。确保模型验证、数据库模式和视图文件中的属性名称相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2018-01-07
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多