2012-06-26 29 views
0

我使用filedrop.js上傳圖像。在application.js文件中的代碼如下所示: -ajax在Rails 3和Ruby 1.9.3之後調用後無法呈現html 1.9.3

$(".droparea").each(function(event){ 
    var game_id = $(this).attr('id') 
    $(this).filedrop({ 
    maxfilezsize: 5, 
    maxfiles: 1, 
    dataType: "script", 
    paraname :"custom_game[file]" 
    url: "/game_plans/'+$(".dropbox").attr("rel")+'/games/'+game_id+/upload_file.js" 
    }) 
} 

這部分工作正常。我正在用適當的參數進入適當的行動。

def upload_file 
    # NOTE Upload_file action has been explicity written for the file upload using drag and drop 
    @custom_game = CustomGame.find_or_initialize_by_id(params[:id]) 
    @game_plan = GamePlan.find(params[:game_plan_id]) 
    @custom_game.update_attributes(params[:custom_game]) 
    respond_to do |format| 
    format.js 
    end 
end 

upload_file.js.erb:

$("#<%= @custom_game%>_file").html(<%= @custom_game.file%>); 

但鑑於各自的行動即upload_file.js.erb我不能執行任何的JavaScript命令,而紅寶石的命令是否正常工作的。並且在日誌中還表示該部分已成功呈現。

我的日誌顯示: -

(101.8ms) COMMIT 
Rendered activities/upload_file.js.erb (0.6ms) 
Completed 200 OK in 379ms (Views: 10.8ms | ActiveRecord: 106.9ms) 

我不知道我在哪裏做的錯誤。 upload_file.js.erb中沒有執行jquery命令,但ruby命令正常工作。

+2

而不是表明*不*工作代碼,請出示代碼*不*工作。即你的控制器和upload_file.js.erb。 – Mischa

+0

感謝Mischa的迴應。我已經提到了控制器的代碼以及js.erb文件。請看一下。 –

+0

不知道這是否會解決您的問題,但您應該使用['escape_javascript'](http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-escape_javascript)來防止問題報價和線路剎車。 – Mischa

回答

1
$("#<%= @custom_game%>_file").html(<%= @custom_game.file%>); 

<%= @custom_game %>返回CustomGame對象,在這裏我想你換貨使用@ custom_game.title什麼的。 jQuery不能找到$(「#< #CustomGame:834793746> _file」),所以html不被替換。

你可能想嘗試在一個控制檯JavaScript來檢查它是否手動執行(使用Chrome的開發工具自己)

+1

是的,我也想知道。也可能''%= @ custom_game.file%>''應該用引號括起來:'.html('<%= @ custom_game.file%>');' – Mischa

+0

感謝你們的幫助......問題解決 –