这是一个复杂的问题,所以我会尽力解释。我有一个附件模型,我使用carrierwave_direct作为gem。 Carrierwave_direct需要在一个页面上上传,然后在另一个页面上上传属性。它需要将您重定向到的另一个页面是编辑页面,但是编辑页面上的属性我无法通过模型进行验证,除非我希望上传失败(因为它不会创建带有针对另一个页面的验证的id )。我想我所说的是如何验证模型属性仅在编辑页面上存在,如果用户退出页面而不填写表单,则删除模型实例。我希望它能够做到这一点,因为我的模型需要它才能在上传时创建模型实例。请参阅下面控制器中的我的代码
def edit
@attachment = Attachment.find(params[:id])
end
def report_uploader
@uploader = Attachment.new.delivery
@uploader.success_action_redirect = uploaded_attachments_url
@uploader
end
helper_method :report_uploader
def after_upload
@attachment = Attachment.new(recruiter_id: current_user.id, uploader_name: current_user.first_name + " " + current_user.last_name)
@recruiter = current_user
@attachment.url = @attachment.delivery.direct_fog_url + params[:key]
@attachment.key = params[:key]
@attachment.filename = File.basename(@attachment.key)
@attachment.save
redirect_to edit_attachment_path(id: @attachment.id, :anchor => 'reports')
end
附件模式
.head
%h2 Add Attachment
.close
%a{:href=>"#",:rel=>"modal:close"} ❎
.content
= direct_upload_form_for report_uploader , :html => { :class => "standard" } do |f|
.fieldgroup.upload
.field
.upload
= link_to "#", class: "btn primary" do
= image_tag 'loader-white.gif', alt: 'Loading', class: 'loading'
Upload Attachment
= f.file_field :delivery, class: 'file_upload', id: 'upload_attachment'
编辑页面(用户重定向到此处)
.content
%h2 Add Some Additional Stuff to your Attachment!
= form_for @attachment, html: {class: "standard manage-job"} do |f|
.fieldgroup.upload
.field
Your Attachment Title
%br
%a{href: attachment_path(id: @attachment.id), target: '_blank' }
= @attachment.filename
.field
%select{name: "attachment[attachment_type]"}
%option{value: ""} Select Attachment Type
%option{value: "Type A"} Type A
%option{value: "Type B"} Type B
%option{value: "Type C"} Type C
.field
Description of your Attachment
= f.text_area :description
.actions
= f.submit "Submit Report", class: "btn primary"
= link_to 'Cancel', attachments_path