0

我有rails 3.1,當我使用ajax進行刪除操作時,頁面沒有顯示更新的內容而沒有手動刷新。在使用rails的ajax調用後,頁面不顯示更新的信息

奇怪的是,所有.js.erb代碼似乎正在正確觸發。我甚至嘗試在destroy.js.erb中放入一個簡單的page.alert('hi'),但刪除後我沒有收到任何警報。我看到一些地方提到在視圖中爲ajax添加一個「腳本」標記以使jquery工作,但最新的教程沒有提到這一點。我會感謝你的幫助。

這是我看到在服務器日誌:

Started DELETE "/categories/35" for 127.0.0.1 at 2011-12-19 12:58:15 -0500 
    Processing by CategoriesController#destroy as JS 
    Parameters: {"id"=>"35"} 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1 
    Category Load (0.3ms) SELECT "categories".* FROM "categories" WHERE "categories"."user_id" = 5 AND "categories"."id" = ? LIMIT 1 [["id", "35"]] 
    SQL (0.5ms) DELETE FROM "categories" WHERE "categories"."id" = ? [["id", 35]] 
Rendered categories/destroy.js.erb (0.5ms) 
Completed 200 OK in 161ms (Views: 30.7ms | ActiveRecord: 3.2ms) 

以下是我在生成的HTML頁面輸出頁面的源代碼,請參閱:

... 
<tr id = "category_35"> 
    <td>High-end products</td> 
    <td class="deletebutton"><a href="/categories/35" data-confirm="Are you sure?" data-method="delete" data-remote="true" rel="nofollow">Delete</a></td> 
    </tr> 

<tr id = "category_36"> 
    <td>Economy products</td> 
    <td class="deletebutton"><a href="/categories/36" data-confirm="Are you sure?" data-method="delete" data-remote="true" rel="nofollow">Delete</a></td> 
    </tr> 

destroy.js.erb的內容:

('#<%= dom_id(@category) %>').fadeOut(700); 

在Gemfile中:

gem 'jquery-rails' 

在categories_controller.rb:

def index 
     @categories = current_user.categories.all 
    respond_to do |format| 
     format.html # index.html.erb 
     format.json 
     format.js 
    end 
    end 

    def destroy 
     @category.destroy 

    respond_to do |format| 
     format.html { redirect_to categories_url } 
     format.js { 
      render :template => 'categories/destroy.js.erb', :layout => false 
     } 
    end 
    end 

在應用程序/視圖/類別/ index.html.erb

<h2>Categories</h2> 
<table name="categories_list"> 
<%= render "categories_list" %> 
</table>  
<br /> 

在部分_categories_list.html.erb

<% @categories.each do |category| %> 
    <tr id = "<%= dom_id(category)%>"> 
    <td><%= category.name %></td> 

    <td class="deletebutton"><%= link_to 'Delete', category, confirm: 'Are you sure?', method: :delete, :remote => true %></td> 
    </tr> 
<% end %> 
+0

你的ajax調用看起來像 – Rafay 2011-12-19 18:15:20

+0

我沒有放任何單獨的ajax調用代碼。我正在使用不顯眼的方法。正如你所看到的,我把:remote => true放在刪除按鈕上。這有幫助嗎? – Tabrez 2011-12-19 19:16:59

+0

@ 3nigma,我是否需要在application.js中添加任何設置代碼?我看到一些老的教程提到ajax設置,但是我們需要使用最新的rails 3.1嗎? – Tabrez 2011-12-19 19:38:21

回答

1

好吧,爲了防止有人在這個問題上絆倒,我想分享解決方案。有大約3件重要的事情:

  1. destroy.js.erb中的$符號丟失。這裏是更正的代碼:

    $('#<%= dom_id(@category)%>')。fadeOut(700);

  2. 另外,必須指示控制器在響應.js時不要使用默認佈局。這可以在應用控制器本身完成。但我在類別控制器中做到這一點如下:

    respond_to do | format | format.html {redirect_to的categories_url} format.json format.js {渲染 「消滅」,:佈局=>假}

  3. 此外,在迭代期間一個點我混淆了format.json與格式。 JS。他們是兩個不同的東西!因爲,我是RoR的新手,我認爲對Ajax調用的響應將是json,而format.json與格式類似。 JS。

最後,我應該給一個喊出來馬克,誰在GitHub上共享一個Ajax演示: https://github.com/markusproske/ajax_demo/tree/jquery

額外的變化,我不得不做,使之對我來說是禁用的佈置工作再現。否則,該演示是徹底的。我仍然在尋找處理js調用的錯誤條件。

相關問題