我正在尝试使用以下代码删除帖子:
<%= link_to 'Destroy', post, :method => :delete, :onclick => "return confirm('Are you sure you want to delete this post?')" %>
不起作用......它只是将我重定向回帖子(posts/:id}
但是,如果我使用以下代码,则可以使用
<%= button_to 'Destroy', post, method: :delete, :onclick => "return confirm('Are you sure you want to delete this post?')" %>
在这种情况下,是否可以使link_to
表现为button_to
?
编辑:销毁控制器功能
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
单击“销毁”按钮时记录:
Started GET "/posts/14" for 127.0.0.1 at 2012-10-21 15:38:28 +0300
Processing by PostsController#show as HTML
Parameters: {"id"=>"14"}
Post Load (0.4ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 14 LIMIT 1
Rendered posts/show.html.erb within layouts/application (0.6ms)
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Completed 200 OK in 7ms (Views: 5.4ms | ActiveRecord: 0.8ms)
[2012-10-21 15:38:28] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
路线:
devise_for :users
resources :users
resources :posts
match '/about' => 'about#index'
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => 'index#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
match '*a', :to => 'error#routing'
答案 0 :(得分:19)
您必须添加
//= require jquery
//= require jquery_ujs
在javascripts / application.js文件中
第二,检查布局文件,
javascript_include_tag "application"
包含与否?
希望得到这个帮助。
答案 1 :(得分:10)
解决方案:
我在创建项目
时从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 .
所以重新添加它解决了问题
答案 2 :(得分:3)
您是否已将此行添加到布局文件中?
<%= javascript_include_tag "application" %>
答案 3 :(得分:1)
这是Rails方式:
<%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %>
如果帖子未删除,则问题出在您的控制器上。您确定已正确实施#destroy
操作吗?您在服务器日志中获得的输出是什么?
更新:将您的操作更改为:
def destroy
@post = Post.find(params[:id])
respond_to do |format|
if @post.destroy
format.html { redirect_to posts_url }
format.json { head :no_content }
else
format.html # do something here
format.json { head :no_content }
end
end
end
答案 4 :(得分:1)
我找到了在没有 jQuery 的情况下为 ruby on rails 创建工作删除链接的最佳过程!我们需要处理 3 件事:
让我们开始...
首先,我们将在def destroy ... end
中添加articles_controller.rb
,
让我们打开:
# app/controllers/articles_controller.rb
def destroy
@article = Article.find(params[:id])
@article.destroy
params[:id] = nil
flash[:notice] = "Art has been deleted"
redirect_to :action => :index
end
这里
这是给出破坏命令的主要因素。并使 link_to 正常工作。
等等 我们需要 2 条销毁页面的路由,它们是:
在路由中添加:
resources :articles, except: [:destroy] # this will add all get request links automatically except destroy link
post '/articles/new' => 'articles#create'
post '/articles/:id' => 'articles#update'
post '/articles/:id/edit' => 'articles#update' # this 3 lines are needed for other forms purpose
# we need this 2 lines for our delete link_to setup
delete 'articles/:id/delete' => 'articles#destroy', as: 'articles_delete'
get '/articles/:id/delete' => 'articles#destroy'
这里
最后第二行声明了 DELETE 方法,
然后
最后一个最甜蜜的删除输出
我们可以使用它来获得正确的删除 link_to 标签,这将起作用。
<%= link_to 'Delete Article', articles_delete_path, method: :delete %>
<%= link_to 'Delete Article', articles_delete_url, method: :delete %>
<% obj.each do |post| %>
<%= link_to 'Delete Article', articles_delete_path(post), method: :delete %>
<% end %>
除 jQuery 之外的已经完成
感谢您正确阅读此答案。
快乐的编程
答案 5 :(得分:0)
确保参数
后没有空格def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path, :notice => "Your post has been deleted successfully."
end
答案 6 :(得分:0)
我在这里查看了所有评论。所有包含都设置正确。但是我注意到删除文章不起作用,删除评论会起作用。在重写了一些代码并在这里和那里进行检查之后,我发现在编辑器中看起来相同的两个文件,但是一个版本可以工作而另一个版本不起作用!
咖喱博客/应用/视图/物品/ index.html.erb:
<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Delete', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
但是,查看带有xxdiff
的文件,我发现在一个版本中只有使用的标签,而另一个版本也使用了空格。可能是从教程中复制粘贴代码。因此,用制表符替换空格可以解决问题。
答案 7 :(得分:0)