Gem carrierwave 多态 has_many nested 关联,取不到 attach

fangxing204 · 2016年01月09日 · 最后由 fangxing204 回复于 2016年01月11日 · 2286 次阅读

相关 model 和 controller

class  Info  < ActiveRecord::Base
  has_many :attachments, as: :attached_item, dependent: :
  accepts_nested_attributes_for :attachments, allow_destroy: true, reject_if: proc { |attributes| attributes[:attachment].nil? }
end
class  Attachment  < ActiveRecord::Base
  mount_uploader :attachment, AttachmentUploader
  # Associations
  belongs_to :attached_item, polymorphic: true

  # Validations
  #validates_presence_of :attachment
  validates_integrity_of :attachment

  # Callbacks
  before_save :update_attachment_attributes

  # Delegate
  delegate :url, :size, :path, to: :attachment

  # Virtual attributes
  alias_attribute :filename, :original_filename

  private
  def update_attachment_attributes
    if attachment.present? && attachment_changed?
      self.original_filename = attachment.file.original_filename
      self.content_type = attachment.file.content_type
    end
  end
end
class  **InfoController**  < ApplicationController
  def create
    info = Info.new(info_params)

    attachs = []
  #  **这里没有执行, 我在控制台,取值是空,单独用attachmentController上传却成功了,且能正常显示**
    info.attachments.each do |a|
      attachs << a.attachment.url
      puts "url ----------------------"      
      puts aattachment.url
    end

    if info.save
      render json: {flag: 1, msg: "file upload success!",attachment_url: attachs}
    else
      render json: {flag: 0, msg: "#{info.errors}, file upload failed,please upload again."}
    end
  end

  private
  def info_params
    params.permit(:title, :content,attachments_attributes: [:attachment, :attachment_cache])
  end
end
#这个上传正常显示
class ** AttachmentController** < ApplicationController
  #skip_before_action :require_logi
  def create
    a = Attachment.create attachment_params
    if a
      render json: {flag: 1, msg: a.url}
    else
      render json: {flag: 0, msg: 'fail'}
    end
  end

  private
  def attachment_params
    params.permit(:attachment)
  end
end
class AttachmentController < ApplicationController
  #skip_before_action :require_login

  def create
    a = Attachment.create attachment_params
    if a
      render json: {flag: 1, msg: a.url}
    else
      render json: {flag: 0, msg: 'fail'}
    end
  end

  private

  def attachment_params
    params.permit(:attachment)
  end
end

谢谢

最后把 InfoController 的 create 改成这样还可以了,主要是我没理解,

info = Info.new
    a = info.attachments.new(params.permit([:attachment]))

    if info.save
      render json: {flag: 1, msg: "file upload success!",attachment_url: a.url}
    else
      render json: {flag: 0, msg: "#{info.errors}, file upload failed,please upload again."}
    end

store_dir是怎么写的?我才你跟 module_name 相关。

#2 楼 @yfractal ,store_dir 用的默认,我现在只能不能 Info.new(),直接一步创建,只能先 new Info,再 new Attachments

#4 楼 @rei 谢谢,我发现错误在提交表单 因为我不用 erb,所以要这样

更新一下: 请求参数这样写 attachments_attributes[attachment],会出现 undefined method `[]' for #<ActionDispatch::Http::UploadedFile, 要这样 attachments[attachment]

param 这样写也会出错 petmit(attachments => [:attachment])
要这样 permit(:attachments => [])

原因我也不知道,这些是我在 stackoverflow 上参考的,希望对大家有帮助

需要 登录 后方可回复, 如果你还没有账号请 注册新账号