简体   繁体   English

可能会重定向rails response_to

[英]Too may redirects rails respond_to

I have a controller action method that gets all records of establishments from the DB, I then want to share this response with a external entity which is a RhoMobile application, i used respond_to to format the response to JSON. 我有一个控制器操作方法,该方法从DB获取机构的所有记录,然后我想与作为RhoMobile应用程序的外部实体共享此响应,我使用了response_to格式化对JSON的响应。

def index
  @establishments = Establishment.index(params).includes(:assessor)
  @json_establishments = Establishment.all
  respond_to do |format|
    format.html { redirect_to(establishments_url) }
    format.json { render json: @json_establishments.as_json }
  end
end

When i navigate to this action i get an error 当我导航到此操作时,出现错误

net::ERR_TOO_MANY_REDIRECTS 净:: ERR_TOO_MANY_REDIRECTS

in chrome developer tools on the console tab. 在控制台标签上的chrome开发人员工具中。

When i remove the { redirect_to(establishments_url) } next to the format.html it's working with a status of 406 (Not Acceptable) but if i would use the search in the action view that i created and click the browsers back button, i get something like: 当我删除format.html旁边的{ redirect_to(establishments_url) } Establishmentments_url { redirect_to(establishments_url) }时,它的工作状态为406(不可接受),但是如果我在创建的操作视图中使用搜索并单击浏览器后退按钮,则会得到就像是:

 ActionController::UnknownFormat in EstablishmentsController#index ActionController::UnknownFormat <div class="source hidden" id="frame-source-0"> <div class="info"> Extracted source (around line <strong>#219</strong>): </div> 

instead and when i refresh the page i get the expected view. 相反,当我刷新页面时,我得到了预期的视图。

No wonder that it is stuck in redirect loop. 难怪它卡在了重定向循环中。

Reason : 原因

establishments_url points to EstablishmentsController#index , and your default format must have been html . establishments_urlEstablishmentsController#index ,和你的默认格式一定是html So, after setting the variables, it redirects to establishments_url , which again tries to load EstablishmentsController#index . 因此,设置变量之后,它重定向到establishments_url ,再次尝试加载EstablishmentsController#index

Solution : 解决方案

Instead of redirecting to the URL, you need to consider rendering a view (as you did in JSON format). 除了重定向到URL外,您还需要考虑呈现视图(就像在JSON格式中所做的那样)。

format.html { render 'establishments/index' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM