2011-05-17 58 views
1

我有一個博客風格的網站,每篇文章下面都有用戶登錄後通常的'查看''編輯'和'刪除'功能。問題是當我點擊'刪除'它發送給你的鏈接到「查看」文章頁面並且不刪除文章。由於刪除和視圖路徑在我的代碼中是不同的,但是做同樣的事情......不能指出這一點。博客刪除操作錯誤?

class ArticlesController < ApplicationController 

    before_filter :authenticate_user!, :except => [:index, :show] 

# GET /articles 
# GET /articles.xml 
# display the amount of article on the home page 

def index 
@articles = Article.published.page(params[:page]).per(6).ordered 

respond_to do |format| 
    format.html # index.html.erb 
# format.atom #index.atom.builder 
    format.xml { render :xml => @articles } 
end 
end 


# GET /articles/1 
# GET /articles/1.xml 
def show 
    @article = Article.find(params[:id]) 
    @comment = Comment.new(:article=>@article) 

respond_to do |format| 
    format.html # show.html.erb 
    format.xml { render :xml => @article } 
end 
end 

... 

/控制器/ articles_controller

# DELETE /articles/1.xml 
def destroy 
@article = Article.find(params[:id]) 
authorize! :destroy, @article 
@article.destroy 

respond_to do |format| 
    format.html { redirect_to(articles_url) } 
    format.xml { head :ok } 
    end 
end 
end 

視圖/物品/ _article.html.erb

<div class= "art-links"> 
<%= link_to 'Read', article %> 
    <% if can? :update, article %> 
| <%= link_to 'Edit', edit_article_path(article) %> |  
<% end %> 

<% if can? :destroy, article %> 
    <%= link_to 'Delete', article, :confirm => 'Are you sure?', :method => :delete %> 
<% end %> 
    </div> 
<br /> 


回答

1

這可能是因爲您沒有設置正確的JavaScript文件。當你點擊鏈接時,Rails 3使用不引人注意的javascript來創建DELETE請求。如果JavaScript不起作用,它將回退到執行GET,這將呈現show操作。

確保您的佈局中有rails.jsprototype.js。如果您使用的不是Prototype的JavaScript框架,您需要使用該框架(jQuery或其他)以及適當的端口rails.js

+0

好點 - 我確實在Railscast插曲後面安裝了「jquery-rails」gem,我的理解是它刪除了prototype.js框架。糖..我如何解決這個問題,所以它再次工作? – ubique 2011-05-17 18:00:13

+0

你在添加gem後運行了'jquery:install'生成器嗎? – 2011-05-17 18:15:26

+0

@Austin我在運行軟件包安裝後運行了(jquery:install)cmd,得到了這個瘋狂的錯誤 - 已經發布了上面的鏡頭。有什麼建議麼? – ubique 2011-05-17 18:44:00

0

此行

format.html { redirect_to(articles_url) } 

應將其發送回您的索引操作,您想在哪裏結束?

+0

我希望它當點擊「刪除」的文章,然後回到指數頁 – ubique 2011-05-17 16:29:37

+0

你可以發佈刪除鏈接的代碼,你的控制器看起來很好 – Nick 2011-05-17 16:46:16

+0

發表了上面的_article.html.erb代碼 – ubique 2011-05-17 17:00:08

0

這種情況也發生在我的項目中。

Finnaly我知道原因。這是因爲我在app/assets/javascript中找到的application.js正在被另一個application.js取代,我嘗試探索bootstap時複製了它。

我只需添加這3排回:

// =需要的jQuery

// =需要jquery_ujs

// =需要的jQuery UI的

到頂行的application.js沒關係。

我的應用程序/視圖/佈局/ application.html.erb

ñ它工作

,並把這個

..萬一

如果jQuery已經安裝ñ仍然錯誤我一樣:d

索裏的英語不好:)

+1

我編輯了一下。不知道你的答案中'n'是什麼意思。是不是'或'和'。請使用完整的英文單詞。 – Jayan 2012-03-30 07:47:42

+0

謝謝你的建議@Jayan,n的意思和。對不起,必須使用n而不是和。謝謝 – ksugiarto 2012-07-23 07:35:56