2

I am trying to Upload videos in my app using Paperclip and jquery-fileupload-rails

I followed ulpoad file with paper clip and jquery to upload video, but was uploading video just as I select the video and don't wait for pressing submit button, So I followed 1: jQuery file upload: Is it possible to trigger upload with a submit button? to select a single video and upload it on submit button, not on selecting file.

But There is issue on whether I put submit button in the form or outside form.

When I put the submit button in the form than it sends two requests to pressing submit button, one with Video and second one without Video.

And If I put button outside form that it sends one request on pressing submit button but after successful create/update it remains on the same page.

All I wanted is that on pressing Submit, sending just one request and redirect to index page on successful create/update.

In _form.slim

= simple_form_for [:admin,add] do |f|
   .wizard-content
     .row
       .col-sm-6
          = f.input :video, multiple: false, wrapper: :horizontal_file_input
          button#submit_button.btn.btn-primary
            | Save

In adds.coffee

 jQuery ->
      $('#add_video').attr('name','add[video]')
      $('#add_video').fileupload
         $('#submit_button').off('click').on 'click', ->
           data.submit()

In controller I have

def create
     if add.save
       redirect_to admin_adds_path
       flash[:success]= "Add Created"
     else
       render :new
     end
   end

   def update
     if add.update(add_params)
       redirect_to admin_adds_path
       flash[:success]= "Add Updated"
     else
       render :edit
     end
   end

there is also a _add.slim

= image_tag(@add.uploaded_file(:small), class: 'thumb')

I also created create.js.erb and edit.js.erb as suggested tutorial I followed (though, I didn't understand its use )

- if @add.new_record?
  |  alert('Failed');
- else
  |  if ($('h1') !== undefined) { $('h1').append("
  =j render partial: 'adds/add', locals: { add: @add }
  | "); }

Whats wrong with this?

4
  • Hi Muhammad. What happens if you comment out the jQuery submit code? Commented Oct 30, 2016 at 3:45
  • @IAMZERG it submitsfrom without showing progress bar Commented Oct 30, 2016 at 9:07
  • Does it still submit twice or just once? Commented Oct 30, 2016 at 15:22
  • just one secod one will be with data.submi() which I removed Commented Oct 30, 2016 at 15:30

2 Answers 2

1

Try the button inside the form and use event.preventDefault(), because it´s seems that the form is doing his default action, that is send data and reload the page and also calling the js.

$('#submit_button').off('click').on 'click', (event) ->
  event.preventDefault()
  data.submit()
Sign up to request clarification or add additional context in comments.

5 Comments

It send request one time but remains on same page, however at console it says Redirected to localhost:3000/admin/adds Completed 200 OK in 3112ms (ActiveRecord: 91.4ms)
You should debug a little to know where is the problem, add some puts in those methods to check if they are running, for instance before the redirect puts ("add________")+add.to_json. Also flash message should be before the redirect.
I have tried a debugger in create/update method, also uses PUT every inch of code is running but still remains on same page
try to change the redirect. redirect_to :root , to know if the code is reaching that line. Make sure you have it set on the routes.rb.
yes it executed, says, Redirected to localhost:3000 Completed 200 OK in 315ms (ActiveRecord: 69.9ms)
1
+50

When you put button outside form you can handle the done event and the redirect. Remember this POST is ajax and if you POST a change or new element you have a response via ajax no via web browser page.

Some like

$('#fileupload').fileupload({
    dataType: 'json',
    add: function (e, data) {            
        $("#up_btn").off('click').on('click', function () {
            data.submit();
        });
        done: function (e, data) {
           window.location.href = "data.next_url"; 
        }
    },
});

Maybe you need to response some json (instead of redirection redirect_to admin_adds_path) to pass from controller to javascript information (some like success,error,next_url,others)

9 Comments

I tried to place button outside from and than used $('#submit_button').off('click').on 'click', (event) -> data.submit() done: (e, data) -> alert("Upload finished.") window.location.href = "data.next_url"; But it seems that its not coming in done: method and stuck on same page
the alert never trigger?
no, record saved page stuck on same page , no alert
you know how debug with the browser console?
can you check the url of the ajax is OK and also the response?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.