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

发布于 2024-12-09 21:05:53 字数 805 浏览 1 评论 0原文

    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 行错误。“登录”按钮或 /logins/new 路径是否有问题?第二个测试通过了,所以路径应该是正确的?我做错了什么?

谢谢!

    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>'

It shows error for line 12. Is there a problem with button "Login" or the /logins/new path? The second test passes though so the path should be correct? What am I doing wrong?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白色秋天 2024-12-16 21:05:53

真的很难说清楚这里发生了什么。一般来说,如果您询问有关路由错误的问题,您也应该发布您的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”的位置。这意味着新对象表单的操作属性应设置为 /tags。那里告诉 Rails 在标签控制器中呈现创建操作。您的登录模型也是如此。

至于您的表单 HTML 代码实际上是什么样子,它可能看起来类似于:

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

何时应该

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

希望这会有所帮助。

It's really hard to tell what's going on here. In general if you are asking a question about a routing error you should post what's in your routes.rb file as well.

That being said, I think whatever HTML gets generated for the form has it's action specified incorrectly.

Example routes:

    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"}

Notice where it says POST in the second column there. That means the action attribute for a new object form should be set to /tags. Having that there tells Rails to render the create action in the Tags controller. The same would be true for your login model.

As far as what your form HTML code actually looks like, it probably looks something along the lines of:

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

When it should be

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

Hope this helps.

后来的我们 2024-12-16 21:05:53

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

I would think that the form in your view file is having a blank action-attribute, therefore it POSTs the form to /logins/new instead of eg. /logins which probably maps to your create-action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文