rails redirect_to格式html在js中发送响应 [英] rails redirect_to format html sends response in js
问题描述
我在我的一些控制器中创建了一个before_filter,用于将关键字搜索重定向到父控制器。
非常简单:
'pre> 的before_filter:redirect_search
DEF redirect_search
redirect_to的控制器: 建筑物,动作: 索引,格式为: HTML 如果PARAMS [:q] 。当下?
end
请注意,keyword_search以js格式发送
一切似乎都奏效。当我查看服务器时,我可以看到buildings / index已经运行并且页面已呈现,但浏览器中没有任何反应。
在浏览器的控制台I看到这个
GET http:// localhost:3000 / buildings.html 200 OK
它在响应正文中有html页面。
这意味着buildings / index是以html的形式运行,然后作为js发送到浏览器。
为什么会这样呢?如何解决它?
感谢Bachan的回答,我可以这样解决我的问题:
def redirect_search
render:js => window.location.href ='+ buildings_path +'if params [:q] .present?
end
谢谢!
I made a before_filter in some of my controller to redirect keyword searches to the parent controller
It's very simple:
before_filter :redirect_search
def redirect_search
redirect_to controller: "buildings", action: "index", format: "html" if params[:q].present?
end
Please note that the keyword_search is sent in "js" format
Everything seems to work. When I look at the server, I can see that the buildings/index is run and that the page is rendered but nothing happens in the browser.
In the browser's console I see this
GET http://localhost:3000/buildings.html 200 OK
It has the html page in the response body
This means that buildings/index is run as html but then sent as js to the browser.
Why is that so? How can I fix it?
Thanks to Bachan's answer, I could solve my issue this way:
def redirect_search
render :js => "window.location.href='"+buildings_path+"'" if params[:q].present?
end
Thanks!
这篇关于rails redirect_to格式html在js中发送响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!