2017-04-03 106 views
1

有一個奇怪的問題,試圖通過rails中的AJAX更新搜索結果,在rails中將js.erb加載到頁面上原始文本的<pre>標記中,並且不會觸發任何js文件。js.erb渲染文本不是JS

在指數控制器

if request.format.js? 
    respond_to do |format| 
     format.js { render layout: false } 
    end 
    end 

形式設置火災AJAX與遠程:真正

= form_tag(search_path, id: "form", remote: true, :'data-type' => 'script') do 

index.je.erb

alert('hello'); 
$("#results").innerHTML("<%= escape_javascript(render partial: 'results', locals: { results: @results }) %>"); 

的routes.rb

match 'search', to: 'properties#index', via: :post, defaults: { format: 'js' } 

在路由中,我嘗試過強制js,因爲之前它是JSON,但沒有區別。 從日誌中,控制器接收JS的請求,但我得到這個在Chrome調試器:

(index):69 Resource interpreted as Document but transferred with MIME type text/javascript: "http://localhost:3000/search". 

堅持一個alert("Js is working")作品沒有問題,所以JS在那裏,射擊

林卡住,這應該是工作到現在爲止,我錯過了一些愚蠢的東西,還是我不知道的Rails 5中的新東西?

如果我可以提供任何更多的信息來幫助你halp我請問!

在此先感謝

回答

0

好吧。看起來真的不夠!它確實似乎向控制器提交ajax請求,但控制器確定它是文本。

加入

jQuery -> 
    $('#form').on "change", -> 
    $.ajax '/properties/index', 
    type: 'GET' 
    dataType: 'script' 
    data: $('form').serialize() 

和hyjacking形式提交到純AJAX似乎解決這一切!