0

I have a form that renders its errors via AJAX. The form is properly sending a remote request, and the server is returning javascript:

(function() {
  $("#main").fadeIn(". . .");
}).call(this);

Unfortunately, nothing actually renders in the browser. At first I thought there must be an error in the js code, but when I copy and paste this code into the Firebug console, it properly fades in the new content. The problem seems to be that the browser isn't running the code in the first place. What might cause this problem?

For reference, here's my form markup:

= simple_form_for @user, html: {multipart: true}, remote: true do |f|
  . . .
  .btn.btn--positive = f.submit

And here's the controller action:

def update
  if @user.update(user_params)
    redirect_to @user, notice: t('shared.notices.item_was_successfully_updated', item: @user.name)
  else
    render layout: 'transition.js.coffee', action: 'edit'
  end
end

The error occurs when the update doesn't validate and Rails runs the line render layout: 'transition.js.coffee', action: 'edit'.

11
  • do you use the the postet javascript in the transition.js.coffee? Commented May 7, 2013 at 13:33
  • Try renaming your file transition.js.haml (I guess you're using HAML), but you might have to use js instead of coffee if you do so... Commented May 7, 2013 at 13:36
  • @DennyMueller I do. The js file reads: $("#main").fadeIn <%= yield %> Commented May 7, 2013 at 13:37
  • as far I know u cant use common js in a coffee script. Maybe try to remove the .coffee first and then run it again. And also you need to add a .erb to the field to run the <%= yield %> Commented May 7, 2013 at 13:40
  • I'm using the coffee-views gem which should allow this functionality, but I went ahead and converted to a basic erb to be sure. Unfortunately, I get the same results. The AJAX doesn't render, but if I copy and paste the response code into the console, it fades in as expected. Commented May 7, 2013 at 13:43

0

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.