2011-05-17 62 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