attr_accessor阻止保存模型字段

时间:2017-01-10 22:22:13

标签: ruby-on-rails paperclip

我正在尝试将代码调整为裁剪图像以及一些用户javascript

关注Railscast 184以及其他教程,但无法使其工作。 (这是一件好事,因为它迫使我更好地理解Ruby)

虽然我的问题是:

我正在尝试将裁剪数据保存到我的数据库中,但发生了奇怪的事情:以下位attr_accessor :x, :y, :width, :height阻止控制器保存这些值。

控制器更新操作

if params[:profilepicupload] then
      newpic = @professionnel.build_profilepic
      newpic.image = params[:profilepicupload]
      newpic.save
    else
      ppic = @professionnel.profilepic
      ppic.x = params[:dataX]
      ppic.y = params[:dataY]
      ppic.height = params[:dataHeight]
      ppic.width = params[:dataWidth]
      ppic.save

    end

Profilepic.rb模型

class Profilepic < ApplicationRecord

  belongs_to :professionnels

  has_attached_file :image, styles: { big: "1200x1200", medium: "400x400", small: "250x250"}, whiny: false, use_timestamp: false, processors: [:cropper]
  validates_attachment :image, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: {less_than: 15.megabytes}
  attr_accessor :x, :y, :width, :height

end

当我删除对模型文件的attr_accessor引用时,x,y,width和height值将保存到模型中!

(在两种情况下都没有完成作物)

1 个答案:

答案 0 :(得分:1)

通过设置自己的attr_accessor方法,您将覆盖Rails逻辑以从数据库中获取这些值。我猜你想要将这些值保存在数据库中。如果是这样,只需通过迁移将它们作为属性添加到模型中,您应该全部设置。

相关问题