[英]Redirect to another page in Ruby on Rails
I want to redirect to another page admin_antenna_reader_rfids_path
at the end of the create
method. 我想在
create
方法的末尾重定向到另一个页面admin_antenna_reader_rfids_path
。 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
: 我收到错误
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".
还要注意,重定向和渲染都不会终止动作的执行,因此,如果要在重定向后退出动作,则需要执行“ redirect_to(...)并返回”之类的操作。
How can I solve this? 我该如何解决?
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. 您必须删除
render json: {status: true}
因为当前您正在尝试使控制器呈现json并同时重定向到HTML页面。 You have to pick one. 你必须选一个。
To handle multiple request format, you can use respond_to 要处理多种请求格式,可以使用response_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. 在
respond_to
块内,您可以根据需要呈现所有请求格式,然后控制器将基于请求标头选择相应的逻辑来响应您。
在一个方法中,您不能渲染或返回多次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.