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?

6
  • You should wrap the render 'partialname' part in brackets. Alternatively, just try <%=j render 'shared/chap_comment_complete' %> Commented Oct 2, 2014 at 15:42
  • Hi, thanks mccannf. Have also just read this, 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. Commented Oct 2, 2014 at 15:50
  • What does your rails log show? Is it rendering the create.js.erb? Commented Oct 2, 2014 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. Commented Oct 2, 2014 at 16:12
  • Not sure, but I thought the remote: true part should be in the form_for declaration, not the submit. Commented Oct 2, 2014 at 16:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

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.