我在我的一些 Controller 中创建了一个 before_filter 以将关键字搜索重定向到父 Controller
很简单:
before_filter :redirect_search
def redirect_search
redirect_to controller: "buildings", action: "index", format: "html" if params[:q].present?
end
请注意,keyword_search 以“js”格式发送
似乎一切正常。当我查看服务器时,我可以看到 buildings/index 正在运行并且页面已呈现,但浏览器中没有任何反应。
在浏览器的控制台中我看到了这个
GET http://localhost:3000/buildings.html 200 OK
它在响应正文中有html页面
这意味着 buildings/index 作为 html 运行,然后作为 js 发送到浏览器。
为什么会这样?我该如何解决?
最佳答案
试试
def redirect_search
respond_to do |format|
format.html {redirect_to buildings_path} if params[:q].present?
format.js {render :js => "window.location.href='"+buildings_path+"'"} if params[:q].present?
end
end
关于javascript - rails redirect_to 格式 html 在 js 中发送响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629811/