Reputation: 67
I want to redirect to another page admin_antenna_reader_rfids_path
at the end of the create
method. I did:
def create
@antenna_reader_rfid = AntennaReaderRfid.new(antenna_reader_rfid_params)
if @antenna_reader_rfid.save
render json: {status: true}
redirect_to admin_antenna_reader_rfid_path(q@antenna_reader_rfid)
else
render json: {errors: @antenna_reader_rfid.errors.full_messages, status: false}
end
end
I get an error AbstractController :: DoubleRenderError
:
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
How can I solve this?
Upvotes: 0
Views: 2301
Reputation: 957
To handle multiple request format, you can use respond_to
if @antenna_reader_rfid.save
respond_to do |format|
format.json { render json: { status: true } }
format.html { redirect_to where_you_want_path }
end
else
# same way as above
end
Within the respond_to
block, you can render all the request formats as you want, then based on the request header, the controller will choose the corresponding logic to respond to you.
Upvotes: 1
Reputation: 5313
You have to remove the line render json: {status: true}
as currently you're trying to make your controller render a json and redirect to an HTML page at the same time. You have to pick one.
Upvotes: 2