0

I have a button that submits a form remotely with Ajax using the rails built in helpers:

  button_to 'Delete Document', [:proofreader, proofreading_job, proofread_document], 
               method: :delete, 
               remote: true,  
               class: "btn btn-danger btn-sm", 
               data: { disable_with: "Deleting document..." }

When the form is submitted to the controller I can cause an alert to show as follows:

def destroy
  @proofread_document.destroy
  respond_to do |format|
    format.js { render js: "alert('The username to be displayed is:')"}
  end
end

However when I want to render the destroy.js.erb file which has no code in it yet I get the following error in the browser:

def destroy
  @proofread_document.destroy
  respond_to do |format|
    format.js
  end
end

Uncaught SyntaxError: Unexpected token <
    at processResponse (rails-ujs.self-ed0b535c2816e34ce8dee0346bd17387dffd4873cb347fa9a8b267dae6f7f41b.js?body=1:257)
    at rails-ujs.self-ed0b535c2816e34ce8dee0346bd17387dffd4873cb347fa9a8b267dae6f7f41b.js?body=1:186
    at XMLHttpRequest.xhr.onreadystatechange (rails-ujs.self-ed0b535c2816e34ce8dee0346bd17387dffd4873cb347fa9a8b267dae6f7f41b.js?body=1:241)

How can I fix this problem its stopped all development on this project.

2

2 Answers 2

1

the < charecter comes from layout file. You can use layout: false with render.

def destroy
  @proofread_document.destroy
  respond_to do |format|
    format.js {render layout: false}
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Saved me a lot of frustration. Thank you
0

Just use layout: false

def destroy
  @proofread_document.destroy
  respond_to do |format|
    format.js { render js: "alert('The username to be displayed is:')", layout: false }
  end
end

Worked fine:

enter image description here

Comments

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.