ActionController :: RoutingError:没有路由匹配[POST]

时间:2011-10-13 21:31:49

标签: ruby-on-rails integration-testing capybara actioncontroller

    require 'test_helper'

     class MyTest < ActionController::IntegrationTest

      test "view posts from login page" do
      visit("/logins/new")
      find_field('Username').set('abode')
      find_field('Password').set('efghi')
      click_link_or_button('Login')
      assert page.has_content?('Signed in!')
      end

      test "go to new user page" do
        visit("/logins/new")
        click_link("New user?")
        assert (current_path == "/users/new")
      end

    end

   Error:
test_view_posts_from_login_page(MyTest):
ActionController::RoutingError: No route matches [POST] "/logins/new"
    test/integration/view_posts_test.rb:12:in `block in <class:MyTest>'

它显示第12行的错误。按钮“Login”或/ logins / new路径是否有问题?第二次测试通过,所以路径应该是正确的?我做错了什么?

谢谢!

2 个答案:

答案 0 :(得分:3)

很难说出这里发生了什么。一般来说,如果你问一个关于路由错误的问题,你也应该发布在routes.rb文件中的内容。

话虽如此,我认为无论为表单生成什么HTML都会错误地指定它的操作。

示例路线:

    tags GET    /tags(.:format)                {:action=>"index", :controller=>"tags"}
         POST   /tags(.:format)                {:action=>"create", :controller=>"tags"}
 new_tag GET    /tags/new(.:format)            {:action=>"new", :controller=>"tags"}
edit_tag GET    /tags/:id/edit(.:format)       {:action=>"edit", :controller=>"tags"}
     tag GET    /tags/:id(.:format)            {:action=>"show", :controller=>"tags"}
         PUT    /tags/:id(.:format)            {:action=>"update", :controller=>"tags"}
         DELETE /tags/:id(.:format)            {:action=>"destroy", :controller=>"tags"}

注意它在第二列中的POST说明。这意味着新对象表单的action属性应设置为/ tags。有那里告诉Rails在Tags控制器中呈现创建操作。您的登录模型也是如此。

就你的表单HTML代码的实际情况而言,它可能看起来像是:

<form ... action="/logins/new" ...>...</form>

什么时候应该

<form ... action="/logins" ...>...</form>

希望这有帮助。

答案 1 :(得分:0)

我认为您的视图文件中的表单具有空白action - 属性,因此它将表单POST到/logins/new而不是例如。 /logins可能会映射到您的create - 操作。