2012-01-12 38 views
1

在我的Rails 3應用程序中,我允許用戶向他們的配置文件添加獎勵。使用Rails UJS我得到了create與ajax一起工作。但是,我不能讓我的生活得到destroy工作。看看我的服務器日誌,它看起來像它的工作,但@award不會被刪除,除非我刷新我的個人資料頁面。使用ajax(destroy.js.erb)不能正常工作的JavaScript刪除對象

我使用的代碼來自於我在Beginning Rails 3中關注的教程。如果有人能幫我弄清楚我的代碼中發生了什麼問題,我會很感激。

awards_controller.rb

def create 
    @award = Award.new(params[:award]) 
    if @award.save! 
    respond_to do |format| 
     #format.html { redirect_to profile_path(@profile) } 
     format.js { } 
    end 
    else 
    respond_to do |format| 
     #format.html { redirect_to profile_path(@profile)} 
     format.js { render 'fail_create_award.js.erb' } 
    end 
    end 
end 

def destroy 
    @award = Award.find(params[:id]) 
    @award.destroy 
    respond_to do |format| 
    #format.html { redirect_to profile_path(@profile) } 
    format.js { render :nothing => true } 
    end 
end 

create.js.erb

$('ul#awardInfo').append("<%= escape_javascript(render(@award)) %>"); 
$('#new_award')[0].reset(); 

destroy.js.erb

$("ul#awardInfo<%= (@award) %>").remove(); 

我的形式刪除獎,這下一個顯示爲 「X」 來該獎項屬於/由創建:

<li>-&nbsp;<%= award.body %><% if @profile = current_user.profile %><span class="delete"><%= link_to 'x', award_path(award), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this award?", :class => "delete_award" %></span><% end %></li> 

在我的服務器日誌,當我點擊 「刪除」(但刷新頁面前):

Started DELETE "/awards/8" for 127.0.0.1 at 2012-01-11 21:34:58 -0500 
    Processing by AwardsController#destroy as JS 
    Parameters: {"id"=>"8"} 
    Award Load (1.0ms) SELECT "awards".* FROM "awards" WHERE "awards"."id" = '8' LIMIT 1 
    SQL (0.2ms) BEGIN 
    SQL (1.4ms) SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull 
FROM pg_attribute a LEFT JOIN pg_attrdef d 
ON a.attrelid = d.adrelid AND a.attnum = d.adnum 
WHERE a.attrelid = '"awards"'::regclass 
AND a.attnum > 0 AND NOT a.attisdropped 
ORDER BY a.attnum 

    AREL (0.6ms) DELETE FROM "awards" WHERE "awards"."id" = 8 
    SQL (2.3ms) COMMIT 
Rendered text template (0.0ms) 
Completed 200 OK in 31ms (Views: 1.3ms | ActiveRecord: 5.4ms) 

更新:如果我從我的摧毀行動刪除render :nothing => true,這不是」實際上取消了獎項。我的日誌顯示如下:

Started DELETE "/awards/3" for 127.0.0.1 at 2012-02-03 07:54:34 -0500 
    Processing by AwardsController#destroy as JS 
    Parameters: {"id"=>"3"} 
    Award Load (238.1ms) SELECT "awards".* FROM "awards" WHERE "awards"."id" = '3' LIMIT 1 
    SQL (0.4ms) BEGIN 
    SQL (1.6ms) SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull 
FROM pg_attribute a LEFT JOIN pg_attrdef d 
ON a.attrelid = d.adrelid AND a.attnum = d.adnum 
WHERE a.attrelid = '"awards"'::regclass 
AND a.attnum > 0 AND NOT a.attisdropped 
ORDER BY a.attnum 
    AREL (97.3ms) DELETE FROM "awards" WHERE "awards"."id" = 3 
    SQL (45.8ms) COMMIT 
Rendered awards/destroy.js.erb (1.1ms) 
Completed 200 OK in 954ms (Views: 531.9ms | ActiveRecord: 383.2ms) 

更新2:所以,如果我去檢查HTML一個奇怪的事情發生了:

  • 如果我查看源代碼,我實際上沒有看到任何的獎項。
  • 如果我檢查的元素,我看到以下內容: <li>-&nbsp;test<span class="delete"><a href="/awards/4" class="delete_award" data-confirm="Are you sure you want to remove this award?" data-method="delete" data-remote="true" rel="nofollow">x</a></span></li>

<li>是創建實際的獎勵。我有一個award.rb模型,belongs_to :profile。如果個人資料大獎屬於我的個人資料,我會看到一個刪除選項,這是<span>的用途。

回答

3

destroy行動把format.js { render :nothing => true }據推測告訴滑軌連接到使您的destroy.js.erb

使用相同format.js { }create動作應該指示的Rails用於呈現destroy.js.erb並隨後執行所需的操作。

更新

從上面的代碼,它看起來像有什麼在可用於選擇上爲remove呼叫實際獎HTML。

我建議確保li元素包含帶有獎ID的ID屬性。喜歡的東西:

<li id="award-<%= @award.id %>"> 

然後,除去獎項,選擇其li元素,並刪除如下:

$('li#award-<%= @award.id %>').remove(); 

它看起來對我來說,唯一剩下的問題是,jQuery的呼叫不是選擇正確的元素或任何元素來調用remove。給li一個唯一的ID,然後選擇要刪除的ID 應該使destroy操作正確呈現。

+0

不幸的是取出'render:nothing => true'並沒有辦法。 – tvalent2 2012-02-02 13:14:54

+0

這樣做是否會改變日誌中的「渲染文本模板」行?當你執行'render:nothing => true'時,Rails渲染一個空的文本模板。只是做'format.js'應該改變了Rails的渲染方式,如果可以的話,我可以提供更多的幫助,如果我看到現在正在渲染的東西。 – Ryan 2012-02-02 22:37:45

+0

我更新了日誌顯示的問題。現在至少我的'destroy.js.erb'正在渲染。所以它必須是該文件中的內容? – tvalent2 2012-02-03 12:58:15