簡體   English   中英

重定向到Ruby on Rails中的另一個頁面

[英]Redirect to another page in Ruby on Rails

我想在create方法的末尾重定向到另一個頁面admin_antenna_reader_rfids_path 我做了:

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

我收到錯誤AbstractController :: DoubleRenderError

在此操作中,多次調用了渲染和/或重定向。 請注意,您只能調用渲染或重定向,每個操作最多只能調用一次。 還要注意,重定向和渲染都不會終止動作的執行,因此,如果要在重定向后退出動作,則需要執行“ redirect_to(...)並返回”之類的操作。

我該如何解決?

您必須刪除render json: {status: true}因為當前您正在嘗試使控制器呈現json並同時重定向到HTML頁面。 你必須選一個。

要處理多種請求格式,可以使用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

respond_to塊內,您​​可以根據需要呈現所有請求格式,然后控制器將基於請求標頭選擇相應的邏輯來響應您。

在一個方法中,您不能渲染或返回多次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM