ruby-on-rails
  • ruby
  • ajax
  • ruby-on-rails-3
  • link-to
  • 2011-04-01 64 views 8 likes 
    8

    我確信一個快速簡單的答案。 我正在將Rails項目從版本2升級到版本3,並根據Rails 3更新將link_to_remote的負載替換爲link_to。即使這樣簡單:Rails 2 to Rails 3:使用link_to而不是link_to_remote(包括遠程和更新)

    <%= link_to "Check Time", 
         {:action=>:get_time}, :remote=>true, :update=>'current_time' %> 
    <div id='current_time'></div> 
    

    似乎並不奏效。 (使用GET方法)的請求,正在經歷確定並呈現的HTML是:

    <a href="/monitoring/get_time" data-remote="true" update="current_time">Check Time</a> 
    

    的routes.rb項:

    get "monitoring/get_time" 
    

    正如我說,我敢肯定,這是一個非常明顯的問題我這邊!

    回答

    17

    :update選項不受新的link_to :remote => true支持。

    你將不得不

    • 使用legacy plugin
    • 寫JS/AJAX自己,而不是使用:remote => true
    • 使用包含在主要佈局render :update { |page| p.replace_html ... }
    +6

    您不需要完全替換:remote => true。使用它,並綁定來自rails.js的內置回調事件:'ajax:before','ajax:complete','ajax:success','ajax:failure'。 – 2012-06-26 13:55:54

    +0

    文章[「Unobtrusive JavaScript in Rails 3」](http://bit.ly/aCdHWY)有點過時(2010年6月8日),但有一些綁定這些事件的例子。 – 2013-02-08 22:58:42

    7

    :update參數消失。您需要使用Unobtrusive JavaScript自己處理DOM更新。另外,請確保您實際上在佈局中包含csrf_meta_tag助手。

    我寫了一篇關於using unobtrusive JavaScript in Rails 3的文章。

    +1

    csrf_meta_tag的。似乎是Ruby的一個問題,功能幾乎在一夜之間出現和消失。 我可以理解刪除的原因:更新,但它是一個非常有用的代碼! 感謝您的幫助。 – detheridge02 2011-04-01 10:08:32

    +3

    請注意'Rails!= Ruby'。 – 2011-04-01 10:09:15

    +0

    是的,我的意思是Rails :-) 難道你不能告訴我是一個PHP編碼器必須與Ruby握手:-) 偉大的文章的方式! – detheridge02 2011-04-01 10:15:00

    相關問題