Reputation: 4211
I have a jquery ajax call that goes to the create action, and I need to get the response back as JSON to then do something with it on the page in the success ajax function, but for some reason with rails it keeps throwing a missing template error:
class MembersController < ApplicationController
respond_to :json
def create
@member = @group.members.build
@member.user_id = params[:user_id]
respond_with(@member) if @member.save
end
end
Should I be rendering nothing at all?
Upvotes: 4
Views: 4718
Reputation: 3346
I believe you should be using
respond_with(@member)
Removing the conditional.
This will use an "Unprocessable Entity" for json requests with an invalid object.
Upvotes: 7
Reputation: 24174
If @member.save
fails, then the default rendering will take place, which means that rails will try to render create.*.erb
, or create.rjs
, etc. You probably want to do
def create
@member = @group.members.build
@member.user_id = params[:user_id]
if @member.save
respond_with(@member)
else
render :nothing => true
end
end
Upvotes: 4