我有这段代码:
<table>
<% @posts.each do |post| %>
<tr>
<td><%= post.created_at.utc.strftime("%d.%m.%Y") %></td>
<td><%= link_to(post.title, {:controller=>:blog,:action=>:show, :id=>post}) %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', {:controller=>:posts,:action=>:destroy, :id=>post}, :method=>:delete %></td>
</tr>
<% end %>
问题是link_to'Destroy'没有删除帖子,只是打开它们。有什么建议可以解决这个问题吗?
routes.rb文件:
LivuPamatskola::Application.routes.draw do
resources :posts
get "admin/rakstu_red"
get "home/par_skolu"
get "home/personals"
get "home/kontakti"
get "home/pers_pieeja"
get "home/galerijas"
get "home/index"
root :to => 'home#index'
posts.controller.rb destroy section:
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
的application.js:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
布局负责人:
<head>
<title>Līvu pamatskola</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%= stylesheet_link_tag 'style' %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
解决:
在这篇文章中找到解决方案:Rails 3.1 link_to not showing confirmation or destroying properly
无论如何,谢谢你们的帮助!
答案 0 :(得分:2)
<%= link_to 'Destroy', post, :method => :delete, :confirm => "Are you sure?" %>
你也可以清理一点编辑链接
<%= link_to post.title, post %>
编辑链接
<%= link_to 'Edit', [:edit, post] %>
答案 1 :(得分:0)
可能错过了这个:<%= csrf_meta_tag %>
它应该在application.html.erb中,完整的'd be
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
修改强>
您使用的是JQuery吗?如果是这样,我认为问题可能是您在没有更新的rails.js文件的情况下使用JQuery。
在这里下载rails.js:https://github.com/rails/jquery-ujs/raw/master/src/rails.js将它放在你的javascripts目录中,覆盖默认使用rails的rails.js。
添加javascript包含行以包含它。
&lt;%= javascript_include_tag“rails”%&gt; 把它放在你的Jquery包含标签之后。
修改强>
抱歉,我认为你使用的是rails3,这就是为什么当你询问时最好显示rails版本 在rails 3.1或更高版本中,您不需要rails.js,只需确保 application.js
中存在这些行。//= require jquery
//= require jquery_ujs
//= require_tree .