当我点击Destroy链接时,帖子被打开(呈现show动作)而不是被销毁。
视线中的一行:
<%= link_to 'Destroy', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %>
posts_controller.rb片段:
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
耙路线:
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
在routes.rb
中 resources :posts do
resources :comments
end
在我点击destroy链接时的development.log中:
Started GET "/posts/5" for 127.0.0.1 at 2013-12-28 21:51:40 +0000
Processing by PostsController#show as HTML
Parameters: {"id"=>"5"}
[1m[35mPost Load (0.2ms)[0m SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "5"]]
[1m[36mComment Load (0.2ms)[0m [1mSELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ?[0m [["post_id", 5]]
Rendered collection (0.0ms)
Rendered comments/_form.html.erb (7.6ms)
Rendered posts/show.html.erb within layouts/application (12.6ms)
[1m[35mAdmin Load (0.2ms)[0m SELECT "admins".* FROM "admins" WHERE "admins"."id" = 1 ORDER BY "admins"."id" ASC LIMIT 1
Completed 200 OK in 18ms (Views: 15.8ms | ActiveRecord: 0.8ms)
我知道问题可能是因为我在我的routes.rb中使用“resources:posts”但是如何删除帖子呢?
如果您需要任何其他信息,请告诉我们。 感谢
答案 0 :(得分:0)
不应该
<%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
你在做什么:
<%= link_to 'Destroy', post_path(post),method: :delete, data: { confirm: 'Are you sure?' }%>
所以,当你点击你给它的链接post_path(post)
时,这说明路线好了,转到post_path并传入我点击的那个帖子。虽然你应该做的就是给它一个对象post
,然后rails会处理其余部分。
答案 1 :(得分:0)
必须在/layout/application.html.erb
中添加<%= javascript_include_tag 'application' %>
<%= csrf_meta_tag %>