Skip to content

Instantly share code, notes, and snippets.

@mh-github
Created June 1, 2017 19:25
Show Gist options
  • Select an option

  • Save mh-github/f1d0a66f3f3edd509394bfff24e5d202 to your computer and use it in GitHub Desktop.

Select an option

Save mh-github/f1d0a66f3f3edd509394bfff24e5d202 to your computer and use it in GitHub Desktop.
# Offer model code
has_and_belongs_to_many :offer_attachments, join_table: "oa_join"
has_many :offer_random_attachments, :dependent => :destroy
class OfferAttachment < ActiveRecord::Base
has_and_belongs_to_many :offers, join_table: "oa_join"
end
class OfferRandomAttachment < ActiveRecord::Base
belongs_to :offer
has_attached_file :random_attachment, :path => ":rails_root/storage/docs/offers/random_attachments/:id/:filename", :url => "/storage/docs/offes/random_attachments/:id/:filename"
do_not_validate_attachment_file_type :random_attachment
end
# View code
# first use case
<div class="field">
<%= f.label "Attachment to be sent" %><br>
<%= f.collection_check_boxes :offer_attachment_ids, OfferAttachment.all, :id, :name do |b| %>
<div class="collection-check-box">
<%= b.check_box %>
<%= b.label %>
</div>
<% end %>
</div>
# second use case
<div class="control-group">
<%= f.label :offer_random_attachments, :class => 'control-label' %>
<div class="controls">
<%= file_field_tag "random_attachments[]", type: :file, multiple: true %>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment