gpt4 book ai didi

ruby-on-rails - 自定义路由上的 Rails 功能测试

转载 作者:行者123 更新时间:2023-11-28 20:34:10 27 4
gpt4 key购买 nike

我的应用中有以下路线:

                       GET    /admin/comments(.:format)                 {:controller=>"admin/comments", :action=>"index"}
admin_comments POST /admin/comments(.:format) {:controller=>"admin/comments", :action=>"create"}
new_admin_comment GET /admin/comments/new(.:format) {:controller=>"admin/comments", :action=>"new"}
GET /admin/comments/:id(.:format) {:controller=>"admin/comments", :action=>"show"}
PUT /admin/comments/:id(.:format) {:controller=>"admin/comments", :action=>"update"}
admin_comment DELETE /admin/comments/:id(.:format) {:controller=>"admin/comments", :action=>"destroy"}
edit_admin_comment GET /admin/comments/:id/edit(.:format) {:controller=>"admin/comments", :action=>"edit"}
admin_approve_comment /admin/comments/approve/:id {:module=>"admin", :controller=>"admin/comments", :action=>"approve"}
admin_reject_comment /admin/comments/reject/:id {:module=>"admin", :controller=>"admin/comments", :action=>"reject"}

声明为:

  namespace "admin" do

resources :comments

match '/comments/approve/:id' => 'comments#approve', :as => "approve_comment", :module => "admin"
match '/comments/reject/:id' => 'comments#reject', :as => "reject_comment", :module => "admin"
end

和这样的功能测试:

context "a POST to :approve" do
setup do
comment = Factory(:comment)
sign_in Factory(:admin)
post :approve, :id => comment.id
end

should respond_with :success
end

但是,当我运行它时,我得到:

test: a POST to :approve should respond with 200. (Admin::CommentsControllerTest):
ActionController::RoutingError: No route matches {:action=>"approve", :id=>339, :controller=>"admin/comments"}

这是怎么回事?我犯了什么愚蠢的错误?

最佳答案

这些路由对我来说像是成员路由。所以这样路由

  namespace "admin" do    resources :comments do      member do        get :approve        get :reject      end    end  end

这将生成类似/admin/comments/:id/approve 的路由。据我所知,这是铁路方式。

关于ruby-on-rails - 自定义路由上的 Rails 功能测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3062618/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com