2012-07-04 69 views
0

我試圖保持在同一頁上的用戶並執行更新後執行一些JavaScript ..的Rails 3 js.erb從未被稱爲

但是我js.erb似乎永遠不會被調用,而是我重定向到路徑的 「http://本地主機:3000/upload_images/id」 和我的螢火控制檯產生...

"NetworkError: 406 Not Acceptable -http://localhost:3000/upload_images/id" 

更新操作

def update 
    @upload_image = UploadImage.find(params[:id]) 
    #If this image is set as the preview restore all associated images to default 
    if params[:upload_image][:preview] == "1" 
     other_uploads = @upload_image.upload.upload_images.where("not upload_images.id = ?", @upload_image.id) 
     other_uploads.each do |upload_image| 
     upload_image.preview = "0" 
     upload_image.save 
     end 
    end  
    respond_to do |format|  
     if @upload_image.update_attributes(params[:upload_image]) 
     #keep the user on current page and call some Javascript 
     format.js{render :template => 'update.js.erb'} 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @upload_image.errors, status: :unprocessable_entity } 
     format.js 
     end 
    end 
    end 

Update.js.erb

alert("hello world!"); 

編輯形式

<%=form_for(@upload_image) do |f| %> 
    .... 
<% end %> 

我看到實時更新這並不重要,如果用戶刷新看到他們,我不介意。我只想保持頁面狀態(除了JQuery所做的一些更改之外),併發布到數據庫。

回答

2

你可能需要切換到遠程形式:

<%= form_for(@upload_image, :remote => true) do |f| %> 

否則你的形式將嘗試一個期望的HTTP響應的HTTP POST。

+0

嘿馬特格洛弗感謝您的快速反應,工作。 – Noz