简体   繁体   English

重定向到Ruby on Rails中的另一个页面

[英]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.

相关问题 Redirect_to Rails中的另一个页面 - Redirect_to another page in rails 如何在Ruby on Rails中将/ page / index页面重定向到/ page(和/ page重定向到/ page /)? - How to redirect /page/index page to /page (and /page to /page/) in Ruby on Rails? 重定向到具有大数据的另一个端点 - Rails/Ruby - Redirect to another endpoint with large data - Rails/Ruby 如何在 Ruby On Rails 中重定向到上一页? - How to redirect to previous page in Ruby On Rails? 登录后重定向到页面,在Rails中使用ruby - redirect to a page after sign in , in ruby on rails 重定向到Facebook上的新页面就像Ruby on Rails一样 - Redirect to new page on Facebook Like in Ruby on Rails 将管理员用户重定向到不同的页面 - Ruby on Rails - Redirect admin user to Different Page - Ruby on Rails Ruby on Rails按钮将不会重定向到另一个页面,但会在同一视图上执行操作 - Ruby on rails button that will not redirect to another page but does it's action on the same view 在Rails的Ruby(2.2)中,我如何弹出确认框,然后重定向到另一页 - In Ruby (2.2) on Rails How Do I Have A Confirmation Box Pop Up Then Redirect To Another Page 如何使用选择菜单和link_to重定向到另一个页面 - Ruby on Rails - How to redirect to another page using a select menu and link_to - Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM