照着RubyGuide里面敲一遍,前面的还好,直到遇到D:destroy 出问题了。
问题代码:
<td><%= link_to 'Destroy', article_path(article),:method => :delete,data: { confirm: 'Are you sure?' } %></td>
首先, 点击 删除 链接之后,跳到了show页面,完全没有弹出框提示,
原因:jquery的js文件没有引入
解决方法:app/view/layouts/application.html.erb 添加
<%= javascript_include_tag "jquery.min" %> //链接js文件
<%= javascript_include_tag "jquery_ujs" %>
然后报错:
Asset was not declared to be precompiled in production.
Add `Rails.application.config.assets.precompile += %w( jquery.min.js )` to `config/initializers/assets.rb` and restart your server
解决方法:按照提示添加即可
重启,可以弹出窗口了,确定,报错:
ActionController::InvalidAuthenticityToken
解决方法:app/view/layouts/application.html.erb 添加
然后发现跳转还是有问题,
更改articles_controller.rb里面的destroy方法
def destroy
@article=Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
<%= csrf_meta_tag %> 再次尝试,删除成功!