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'.

nullnullnull
  • 8,039
  • 12
  • 55
  • 107
  • do you use the the postet javascript in the transition.js.coffee? – Denny Mueller May 07 '13 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... – Raindal May 07 '13 at 13:36
  • @DennyMueller I do. The js file reads: `$("#main").fadeIn <%= yield %>` – nullnullnull May 07 '13 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 %>` – Denny Mueller May 07 '13 at 13:40
  • I'm using the [coffee-views](https://github.com/yury/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. – nullnullnull May 07 '13 at 13:43
  • I guess you are on 3.2? – Denny Mueller May 07 '13 at 13:52
  • @DennyMueller This is actually using the Rails 4 beta. Might I have encountered an actual bug? – nullnullnull May 07 '13 at 13:58
  • after digging and digging, I cant find something about `render layout: *js.coffee`. try `render :js => 'transition.js.coffee', action: 'edit'` – Denny Mueller May 07 '13 at 14:34
  • Sorry, I should have explained that. The javascript is within a layout file, after a recommendation [here](http://stackoverflow.com/questions/16328472/rails-render-a-view-not-a-partial-from-within-a-view). It works in most cases when I'm using a responder, but here (because I'm making a direct render call) it's causing some problems. Unfortunately, if I just change `layout: 'transition.js.coffee' to `js: `transition.js.coffee`, my response is the text `transition.js.coffee` rather than an execution of the layout file. – nullnullnull May 07 '13 at 14:48
  • mhm I dont have more ideas. Maybe a last one Im wondering about shouldnt it be `render :layout => 'transition.js.coffee'` – Denny Mueller May 07 '13 at 15:06
  • Looks like that renders the same effect. Thanks for all your help though, Denny! – nullnullnull May 07 '13 at 15:21

0 Answers0