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

fangxing204 · 发布于 2016年1月09日 · 最后由 fangxing204 回复于 2016年1月11日 · 637 次阅读
23959

相关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

谢谢

共收到 6 条回复
23959
fangxing204 · #1 · 2016年1月10日

最后把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
7072
yfractal · #2 · 2016年1月10日

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

23959
fangxing204 · #3 · 2016年1月11日

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

23959
fangxing204 · #5 · 2016年1月11日

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

23959
fangxing204 · #6 · 2016年1月11日

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

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

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

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