2014-02-10 45 views
7

我有一個與Rails中發送的形式與遠程真正的4

<%= form_for current_user, url: update_image_user_path(current_user), method: :post, html: {multipart: :true, remote: true}, :authenticity_token => true do |f| %> 

更新映像的形式來操作具有

respond_to do |format| 
     format.js 
     format.html 
    end 

,但我收到以下錯誤

ActionView::MissingTemplate - Missing template users/update_image, application/update_image with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. 

我沒有update_image.html.erb模板,但是從錯誤中我知道請求不是以js格式發送的

我錯過了什麼?

回答

2

html: {multipart: :true}, remote: true而不是html: {multipart: :true, remote: true}怎麼樣?

+0

仍然不會包括在格式JS –

+1

我不是當然。但也許錯誤是由ajax限制造成的。 http://stackoverflow.com/questions/4009252/is-it-possible-in-rails3-to-have-a-multipart-form-true-use-remote-true – ironsand

6

有幾件事情你應該知道。

  1. Ajax使用稱爲xmlhttprequest的東西來發送您的數據。不幸的是,xmlhttprequests無法發佈文件。 Remotipart可能會工作,但jqueryfileupload上有更多的文檔和一個軌道轉換視頻。它被許多網站(很多甚至不使用rails)使用。我會建議使用jqueryfileupload護欄寶石與投導軌沿:

    A. https://github.com/tors/jquery-fileupload-rails
    B. http://railscasts.com/episodes/381-jquery-file-upload

  2. 遠程:真不是HTML屬性。更改代碼壽:

    <%= form_for current_user, url: update_image_user_path(current_user), 
        method: :post, html: {multipart: :true}, remote: true, 
        :authenticity_token => true do |f| %> 
    

您所遇到的錯誤中最重要的部分是這樣的:

:formats=>[:html] 

應該說:

:formats=>[:js] 

帶遙控:真在正確的地方,錯誤應該消失。

4

rails remote true不適用於圖片上傳。你不能用ajax只使用遠程真實圖像來上傳圖像。你需要jquery.form.js文件給用戶ajaxForm。對於這包括

<script src="http://malsup.github.com/jquery.form.js"></script> 

和綁定形式

 $("#formid").ajaxForm() 

或者您可以使用jqueryfileupload護欄寶石

https://github.com/tors/jquery-fileupload-rails