Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

javascript - rails redirect_to format html sends response in js

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try with

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...