In the Update action of Rails controllers usually there is code that looks like this:
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
redirect_to(@book)
else
render :edit
end
end
In the else case, this will render the edit template. But what if I wanted to use a respond_to, exactly the same way that I have in the edit action, as:
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
redirect_to(@book)
else
respond_to do |format|
format.html # edit.html.erb
format.json { render :json => @team }
end
end
end
So, if the Update fails, be sure you are returning a json or html depending on the requested format. Does that makes sense? If so, how would you avoid the error: "Render and/or redirect were called multiple times in this action"
Makes sense to me. The answer should be simple, just
return
afterredirect_to
.Not sure exactly how you're rendering multiple times, but assuming you are, a well-placed
return
should tell RAILS to stop processing any further renders after redirecting. If that's all true, it's likely that there's anafter_filter
interfering from somewhere.