rails redirect_to 格式 html 在 js 中发送响应 [英] rails redirect_to format html sends response in js
问题描述
我在我的一些控制器中创建了一个 before_filter 来将关键字搜索重定向到父控制器
I made a before_filter in some of my controller to redirect keyword searches to the parent controller
很简单:
before_filter :redirect_search
def redirect_search
redirect_to controller: "buildings", action: "index", format: "html" if params[:q].present?
end
请注意keyword_search是以js"格式发送的
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
它在响应正文中有 html 页面
It has the html page in the response body
这意味着建筑物/索引作为 html 运行,然后作为 js 发送到浏览器.
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?
推荐答案
感谢 Bachan 的回答,我可以这样解决我的问题:
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
谢谢!
这篇关于rails redirect_to 格式 html 在 js 中发送响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!