Rails [RSpec]:put:update,no route matches:id =>零

时间:2015-07-24 19:37:33

标签: ruby-on-rails ruby-on-rails-4 rspec devise routes

非常奇怪的错误。我正在为subgroup控制器编写RSpec测试,该控制器与group处于多对一关系。 group接受subgroup的嵌套属性。我使用脚手架,我使用了提供的测试(编辑测试)。在我开始测试Devise之前,这些测试都在进行。现在,我使用无效参数进行更新的测试失败了。

在subgroups_controller_spec.rb中进行测试

let(:invalid_attributes) {
  {name: nil, group_id: nil}
}

context "with invalid params" do
  it "assigns the subgroup as @subgroup" do
    subgroup = Subgroup.create! valid_attributes
    puts "param: #{subgroup.to_param}, id: #{subgroup.id}
    put :update, {:id => subgroup.to_param, :subgroup => invalid_attributes}, valid_session
    expect(assigns(:subgroup)).to eq(subgroup)
  end

  it "re-renders the 'edit' template" do
    subgroup = Subgroup.create! valid_attributes
    put :update, {:id => subgroup.to_param, :subgroup => invalid_attributes}, valid_session
    expect(response).to render_template("edit")
  end
end

错误消息:

1) SubgroupsController PUT #update with invalid params assigns the subgroup as @subgroup
     Failure/Error: put :update, {:id => subgroup.to_param, :subgroup => invalid_attributes}, valid_session
     ActionView::Template::Error:
       No route matches {:action=>"show", :controller=>"groups", :id=>nil, :locale=>nil}

puts "param: #{subgroup.to_param}, id: #{subgroup.id}"打印param: 1, id: 1,所以这些ID不是零。

rake routes:

    subgroups GET      (/:locale)/subgroups(.:format)           subgroups#index 
              POST     (/:locale)/subgroups(.:format)           subgroups#create 
 new_subgroup GET      (/:locale)/subgroups/new(.:format)       subgroups#new 
edit_subgroup GET      (/:locale)/subgroups/:id/edit(.:format)  subgroups#edit
     subgroup GET      (/:locale)/subgroups/:id(.:format)       subgroups#show
              PATCH    (/:locale)/subgroups/:id(.:format)       subgroups#update
              PUT      (/:locale)/subgroups/:id(.:format)       subgroups#update
              DELETE   (/:locale)/subgroups/:id(.:format)       subgroups#destroy

P.S。路线中列出了区域设置,但我将其删除以便于查看。我不认为locales是这个问题,因为更新需要:id来更新。

1 个答案:

答案 0 :(得分:0)

问题不在于您的put路线,而是在渲染中。您收到了模板错误,错误消息表明您正试图在show上执行groups。检查布局是否使用了路径。