1

I have a form partial, within a div with id "chapcomments", with a submit tag -

<%= f.submit "Post", remote: true %>

In the correct view folder, I have create.js.erb -

$('#chapcomments').html("<%=j render 'shared/chap_comment_complete' %>");

And in the controller I have the format block which includes

def create
  @chap_comment = current_user.chap_comments.build(chap_comment_params)

  respond_to do |format|
    if @chap_comment.save
      format.js
      format.html {redirect_to chapter_path(@chap_comment.chapter)}
    else
      format.html { render :new }
      format.json { render json: @chap_comment.errors, status: :unprocessable_entity }
    end
  end
end

...but I'm not getting ajax behaviour...

Where am I going wrong?

dan
  • 1,030
  • 1
  • 9
  • 24
  • You should wrap the `render 'partialname'` part in brackets. Alternatively, just try `<%=j render 'shared/chap_comment_complete' %>` – mccannf Oct 02 '14 at 15:42
  • Hi, thanks mccannf. Have also just read [this](http://stackoverflow.com/questions/8370399/rails-js-erb-file-cannot-find-method-render), so have moved the code to create.js.erb in the correct view folder. Not getting an error now, just not getting ajax behaviour. Most perplexing. – dan Oct 02 '14 at 15:50
  • What does your rails log show? Is it rendering the create.js.erb? – mccannf Oct 02 '14 at 16:05
  • No, it seems to be skipping to the format.html. I've edited the question to include the whole of the controller action. And thanks, btw. – dan Oct 02 '14 at 16:12
  • Not sure, but I thought the `remote: true` part should be in the `form_for` declaration, not the submit. – mccannf Oct 02 '14 at 16:22
  • It's calling it correctly now, but I'm logging an internal error 'ActionView::Template::Error (undefined method `id' for nil:NilClass):' - but I think this is a separate issue - thanks very much for your help :) - do you want to post an answer I can accept? – dan Oct 02 '14 at 16:33

1 Answers1

1

You've made the change so that the JS is in the right js.erb file in the view, but you need to move the remote: true part from the submit tag into your form_for declaration, otherwise the format.html response block will be rendered instead of format.js.

mccannf
  • 16,619
  • 3
  • 51
  • 63