Remotipart JS Not Executing

602 Views Asked by At

I have a form that needs to submit a .csv file to the server and then append the words in it to a textarea in my page. I am using Remotipart to upload the .csv using AJAX but I cannot get the javascript in the server response to execute. Here are the relevant parts of my code:

The Form:

=form_tag(upload_canvas_words_admin_page_widget_widget_instance_path(widget.page, widget),:method=>'post',:remote=>true,:multipart=>true,:class=>"upload_words_csv") do
        = label_tag "Upload File"
        = file_field_tag "file"
        = submit_tag "Upload"

The Controller:

def upload_canvas_words
    @csv_text = params[:file].read
end

The .js.haml file:

= remotipart_response do
    - if remotipart_submitted?
        alert('#{@csv_text}');
        alert('!');
    - else 
        alert('WHYYYYY?');

When I look at the response I see the javascript being wrapped in a bunch of html, which I assume has something to do with the iFrame transport. But the javascript never actually executes.

2

There are 2 best solutions below

0
On

Adding data: {type: :script} to the form should be the fix

0
On

Refer this issue. And try to follow the solution given here.

https://github.com/JangoSteve/remotipart/issues/89

So what happens is that reponse arrives to the browser with html entity (like ") inside the textarea. When the js code for evaluation is extracted the html entities are replaced by theirs respective characters (like " to ').

That's a characteristic of a textarea. So it doesn't get executed