【问题标题】:Active Admin Rails 4 has manyActive Admin Rails 4 有很多
【发布时间】:2013-10-23 23:23:31
【问题描述】:

我正在使用 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| 触发

【问题讨论】:

  • 请发布错误的堆栈跟踪?

标签: ruby-on-rails paperclip activeadmin formtastic


【解决方案1】:

所以问题出在新闻模型上。我认为 accept_nested_attributes_for 因添加强参数而被弃用,但我想我将其添加到新闻模型中解决了我的问题是错误的

accepts_nested_attributes_for :news_images,
                            :reject_if => lambda { |attributes| attributes[:photo].blank? },
                            :allow_destroy => true

【讨论】:

    【解决方案2】:

    最近修复了 Paperclip 4.1 中的另一个错误:https://github.com/thoughtbot/paperclip/issues/1457

    我花了很多时间来追踪这个问题,但最终还是找到了 formtastic 和回形针 4.1 之间的联系。

    对我有用的解决方案是在我的 Gemfile 中切换到回形针的主分支,如下所示:

    gem 'paperclip', github: 'thoughtbot/paperclip'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多