我正在使用 Rails 4、Active Admin 和 Paperclip 来设置 has_many 图像关联。在生成表单的 has_many 部分时,我不断收到错误。目前我正在 nil:NilClass 的未定义方法“+”。这是我的代码:
新闻模型
class News < ActiveRecord::Base
validates :body, presence: true
validates :title, presence: true, length: { maximum: 140 }
has_many :news_images, dependent: :destroy
end
新闻图片模型
class NewsImage < ActiveRecord::Base
belongs_to :news
has_attached_file :photo, styles: {
small: "150x150>",
medium: "300x300>",
large: "600x600>"
}
validates_attachment_presence :photo
validates_attachment_size :photo, less_than: 5.megabytes
end
管理代码
ActiveAdmin.register News do
index do
column :title
default_actions
end
form multipart: true do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "News Details" do
f.input :title
f.input :body, :as => :rich
end
f.has_many :news_images do |p|
end
f.actions
end
controller do
def permitted_params
params.permit news: [:title, :body, news_images: [:photo]]
end
end
end
理想情况下,我希望用户能够将多张图片上传到表单。有没有人遇到过这个问题?
堆栈跟踪显示 insert_tag renderer_for(:new)
在 f.has_many :news_images do |p| 上被触发
最佳答案
所以问题出在新闻模型上。我认为 accepts_nested_attributes_for 因添加强参数而被弃用,但我想我错误地将其添加到新闻模型解决了我的问题
accepts_nested_attributes_for :news_images,
:reject_if => lambda { |attributes| attributes[:photo].blank? },
:allow_destroy => true
关于ruby-on-rails - Active Admin Rails 4 有很多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19554137/