ActiveAdmin 和 Carrierwave 与 :has_many

发布于 2025-01-07 23:04:09 字数 3190 浏览 0 评论 0原文

我现在完全疯了。

我有 2 个模型:项目和屏幕截图:

create_table "projects", :force => true do |t|
  t.string   "name"
  t.text     "description"
  t.boolean  "isactive"
  t.datetime "created_at",  :null => false
  t.datetime "updated_at",  :null => false
  t.string   "slug"
  t.string   "logo"
  t.string   "teaser"
end

add_index "projects", ["slug"], :name => "index_projects_on_slug"

create_table "screenshots", :force => true do |t|
  t.integer  "project_id"
  t.string   "image"
  t.datetime "created_at", :null => false
  t.datetime "updated_at", :null => false
end

项目模型看起来像

class Project < ActiveRecord::Base

  attr_accessible :name, :description, :isactive, :slug, :logo, :teaser, :screenshots_attributes

  scope :isactive, :conditions => ["isactive = ?",true]

  mount_uploader :logo, LogoUploader

  extend FriendlyId
  friendly_id :name, use: [:slugged, :history]

  has_many :screenshots
  accepts_nested_attributes_for :screenshots

end

,屏幕

class Screenshot < ActiveRecord::Base

  belongs_to :project, :polymorphic => true
  mount_uploader :screenshots, ScreenshotUploader

end

截图上传器当前未经编辑:

# encoding: utf-8

class ScreenshotUploader < 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
  #   "/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 => [300, 150]
  # 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

现在我正在尝试使用此表单添加来自 activeadmin 的屏幕截图

form do |f|
  f.inputs "Project Details" do
    f.input :name
    f.input :logo, :as => :file, :hint => f.template.image_tag(f.object.logo.url)
    f.input :teaser
    f.input :description
    f.input :isactive
    f.has_many :screenshots do |s|
      s.input :image, :as => :file
    end
  end
  f.buttons
end

但我得到的是:

NoMethodError (undefined method `screenshots_changed?' for #<Screenshot:0xc940cd0>):

我用这个来玩过去几个小时,但我尝试的一切都不起作用:(

有什么建议吗?

I'm getting totally crazy right now.

I have 2 models: project and screenshot:

create_table "projects", :force => true do |t|
  t.string   "name"
  t.text     "description"
  t.boolean  "isactive"
  t.datetime "created_at",  :null => false
  t.datetime "updated_at",  :null => false
  t.string   "slug"
  t.string   "logo"
  t.string   "teaser"
end

add_index "projects", ["slug"], :name => "index_projects_on_slug"

create_table "screenshots", :force => true do |t|
  t.integer  "project_id"
  t.string   "image"
  t.datetime "created_at", :null => false
  t.datetime "updated_at", :null => false
end

Project model looks like

class Project < ActiveRecord::Base

  attr_accessible :name, :description, :isactive, :slug, :logo, :teaser, :screenshots_attributes

  scope :isactive, :conditions => ["isactive = ?",true]

  mount_uploader :logo, LogoUploader

  extend FriendlyId
  friendly_id :name, use: [:slugged, :history]

  has_many :screenshots
  accepts_nested_attributes_for :screenshots

end

and the screenshot model

class Screenshot < ActiveRecord::Base

  belongs_to :project, :polymorphic => true
  mount_uploader :screenshots, ScreenshotUploader

end

Screenshot Uploader is unedited currently:

# encoding: utf-8

class ScreenshotUploader < 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
  #   "/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 => [300, 150]
  # 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

And now I'm trying to add screenshots from activeadmin using this form

form do |f|
  f.inputs "Project Details" do
    f.input :name
    f.input :logo, :as => :file, :hint => f.template.image_tag(f.object.logo.url)
    f.input :teaser
    f.input :description
    f.input :isactive
    f.has_many :screenshots do |s|
      s.input :image, :as => :file
    end
  end
  f.buttons
end

But all I get is:

NoMethodError (undefined method `screenshots_changed?' for #<Screenshot:0xc940cd0>):

I played around with this for the last hours but nothing I tried worked :(

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星光不落少年眉 2025-01-14 23:04:09

我相信默认的 active_admin form do |f| 需要替换为:

form(:html => { :multipart => true }) do |f|

I believe the default active_admin form do |f| needs to be replaced with:

form(:html => { :multipart => true }) do |f|
忱杏 2025-01-14 23:04:09

您需要取消注释此行 # include CarrierWave::RMagick

You need to uncomment this line # include CarrierWave::RMagick

再浓的妆也掩不了殇 2025-01-14 23:04:09

我也有同样的问题。确保您运行了迁移 (rake db:migrate)

I had the same problem. Make sure you ran the migration (rake db:migrate)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文