In a recent project i have to cope with image upload, but using a gem like paperclip or carrierwave seemed overloaded to be. It is just a simple logo upload for companies. So i decided to upload the logo as blob described in this post.
But i had one problem: my uploading view is a popup. So i have to use javascript instead of html to proceed the form data. Therefore i made my form remotely.
in views/logos/new.html.erb
<%= form_for [@retailer, @logo], url: retailers_retailer_logo_path(@retailer, format: :js), remote: true, html: {multipart: true} do |f| %> <%= f.file_field :image_file, accept: 'image/png,image/gif,image/jpeg' %> <%= f.submit "cancel", class: "cancel fbclose" %> <%= f.submit "submit", class: "submit fbclose" %> <% end %>
and my controller responds to js
in controllers/logos_controller.html.erb
def create respond_to do |format| if @retailer.create_logo(logo_params) format.html {redirect_to edit_retailers_retailer_path(@retailer) } format.js {} else format.html {render :new } format.js {} end end end
But when i tried it, i got an error saying: ActionController::ParameterMissing (param is missing or the value is empty: logos) though i had strong_parameters implemented for logos and image_file. What was the problem?
Ajax was the problem as explained here. I came up with using a gem called remotipart. With this gem installed you have nothing to change, so my views/logos/create.js.erb file is dead simple as follows:
$.fancybox.close(); $('#ihre-daten').find('form').parent('div').html("");
Yeah i used fancybox2 for the popup.