【发布时间】:2012-10-21 10:52:46
【问题描述】:
我正在尝试使用以下代码删除帖子:
<%= 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'
【问题讨论】:
-
你能确认你的 Gemfile 中有 jquery-rails gem 吗?如果是,您是否还可以通过您的网络检查器确认它已发送到客户端浏览器。
-
是的,所以当我创建 rails 项目时,我从 app.js 中删除了 //= 行,这导致了问题...谢谢!
标签: ruby-on-rails forms link-to