Railscasts #71:尝试在 post 方法中传递参数时出现路由错误

发布于 2024-11-06 07:33:06 字数 1291 浏览 0 评论 0原文

我正在使用 RSpec-2 来测试我的控制器,但我看到了奇怪的行为。 本质上,我所做的与 Railscasts #71 中的操作相同,

describe DeliverablesController do
  describe "responding to POST create" do
    describe "with valid parameters" do
      it "should pass the params to the deliverable item" do
        post :create, :deliverable => {:title => "Some Deliverable"}
        assigns[:deliverable].title.should == "Some Deliverable"
      end
    end
  end
end

结果是以下错误:

  1) DeliverablesController responding to POST create with valid parameters should pass the params to the deliverable item
     Failure/Error: post :create, :deliverable => {:title => "Some Deliverable"}
     ActionController::RoutingError:
     No route matches {:deliverable=>{:title=>"Some Deliverable"}, :controller=>"deliverables", :action=>"create"}

显然 :deliverable 不应成为路由的一部分,而应写入 params 中。我的问题始于不确定调用哪个 post 方法。

我的 Gemfile.lock 可以在此处找到。


EDIT

我的routes.rb可以在这里找到。

I am using RSpec-2 to test my controller and I am seeing weird behavior.
Essentially I am doing the same as in Railscasts #71

describe DeliverablesController do
  describe "responding to POST create" do
    describe "with valid parameters" do
      it "should pass the params to the deliverable item" do
        post :create, :deliverable => {:title => "Some Deliverable"}
        assigns[:deliverable].title.should == "Some Deliverable"
      end
    end
  end
end

Which results in the following error:

  1) DeliverablesController responding to POST create with valid parameters should pass the params to the deliverable item
     Failure/Error: post :create, :deliverable => {:title => "Some Deliverable"}
     ActionController::RoutingError:
     No route matches {:deliverable=>{:title=>"Some Deliverable"}, :controller=>"deliverables", :action=>"create"}

Obviously :deliverable should not be part of the route but written into params. My problem starts with not being sure which post method is called.

My Gemfile.lock can be found here.


EDIT

And my routes.rb can be found here.

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

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

发布评论

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

评论(1

浮云落日 2024-11-13 07:33:06

有时,激发想法是件好事:我的问题是我处于嵌套路线中,基本上执行以下操作。

resources :projects do
    resources :deliverables
end

错误消息让我很困惑,真正的问题是我没有提供 project_id。我通过这样做

post :create, :project_id => 11, :deliverable => {:title => "Some Deliverable"}

而不是

post :create, :deliverable => {:title => "Some Deliverable"}

谢谢你,哈马尔!

Sometimes it is good to bounce off ideas: My problem was that I am in a nested route basically doing the following.

resources :projects do
    resources :deliverables
end

The error message threw me off, the real problem was that I did not provide a project_id. I fixed it by doing

post :create, :project_id => 11, :deliverable => {:title => "Some Deliverable"}

instead of

post :create, :deliverable => {:title => "Some Deliverable"}

Thank you, hammar!

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