dan
dan

Reputation: 1080

Rails - adding ajax to partials, 'undefined method `render''

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?

Upvotes: 1

Views: 461

Answers (1)

mccannf
mccannf

Reputation: 16659

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.

Upvotes: 1

Related Questions