ruby on rails - How do i set a parent_id using s3_direct_upload gem? -
newbie on rails 3.2.14, testing on heroku, uploading image files s3 s3_direct_upload gem.
i've got gem working fine. drag , drop, progress bars , direct upload s3 work great. upload tied attachment model. new record created on upload. fine far.
the issue i'm having want set parent_id
field of new record don't know how pass in. gem doesn't use conventional form submit upload, it's handled via javascript:
<%= s3_uploader_form callback_url: attachments_url, callback_param: "attachment[attachment_url]", id: "mys3uploader" %> <%= file_field_tag :file, multiple: true %> <% end %>
the create action in attachments_controller.rb
:
def create @attachment = attachment.create(params[:attachment]) end
calls javascript view create.js.erb
:
<% if @attachment.new_record? %> alert("failed upload attachment: <%= j @attachment.errors.full_messages.join(', ').html_safe %>"); <% else %> $("#attachments").prepend("<%= j render(@attachment) %>"); <% end %>
which checks if record save worked , adds new upload thumbnail current view.
since i'm not dealing regular form or create
action i'm bit stumped.
i can set default values in attachment model before_save
:
before_save :set_defaults def set_defaults self.name = "photo" self.parent_id = 1 end
but parent_id
defined on page uploads done, during creation of parent object (i.e. blog post).
i know should using has_many
, belongs_to
, they're set , working fine since i'm not using conventional form submit data don't know how assign parent_id.
i've tried passing parent_id
in form , accessing in model:
<%= s3_uploader_form callback_url: attachments_url(:parent_id => 99), callback_param: "attachment[attachment_url]", id: "mys3uploader" %>
it didn't work , know i'm not supposed accessing params inside model anyway.
any me past point appreciated.
ok, read lot of questions , answers on here , pieced myself.
so on right track passing in (:parent_id => 99)
shouldn't have been trying use in model, should using in controller.
i changed create
method in controller fetch draft parent
object had created , make new attachment
record child of it:
def create @parent = parent.find(params[:parent_id]) @attachment = @parent.attachments.create(params[:attachment]) end
the value of :parent_id
passed in coming 'new' parent form (:parent_id => @parent.id)
.
it's working fine now. utter lack of experience holding me :)
Bet365 Casino Mobile App - Get the best promotions
ReplyDeleteBet365 일본야구 분석 사이트 Casino Mobile App. 텍사스 홀덤 In 먹튀검증 this article, I'll try to 레드벨벳러시안룰렛 explain how it works for you and how 바카라커뮤니티 to get the best promotions. How to Download and Install Bet365 Mobile App.