在使用 Rails 3.2 时遇到的这个问题,我一直在努力解决问题。我有一个简单的集成测试可以做到这一点。
require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
fixtures :posts, :comments
test "should delete comment" do
post = posts(:one)
comment = comments(:one)
delete post_comment_path, :post_id => post.id, :id => comment.id
assert_response :redirect
assert_assign (:post)
end
end
您可以从 Rails 指南中识别模型。无论如何,当我打电话时,rake:integration
我最终得到了这个。
test_should_delete_comment(UserFlowsTest):
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"comments"}
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:533:in `raise_routing_error'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:529:in `rescue in generate'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:521:in `generate'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:562:in `generate'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:587:in `url_for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/url_for.rb:148:in `url_for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:213:in `post_comment_path'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/testing/integration.rb:382:in `method_missing'
C:/dev/blog/test/integration/user_flows_test.rb:9:in `block in <class:UserFlowsTest>'
我的路线是这样定义的。
root / home#index
posts_search GET /posts/search(.:format) posts#search
home_index GET /home/index(.:format) home#index
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
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
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
我究竟做错了什么?
编辑:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
end
不知道这里可能出了什么问题。