0

我有一个decisions资源,它嵌套在一个groups资源下。has_manybelongs_to已在模型中定义。

resources :groups do
  resources :decisions
end

...我在这条路径上有一个编辑表单:

/groups/:group_id/decisions/:id/edit(.:format)

我的 Rspec 测试出现错误:

失败/错误:put :update, {:id => decision.to_param, :decision => valid_attributes, group_id: decision.group.id}, valid_session NoMethodError: undefined method `decision_url' for #DecisionsController:0x007ffeb23482e0>

当我在开发环境中导航到表单时,我收到了类似的错误:

<%= link_to 'Edit', edit_group_decision_path(@group, @decision) %>

/groups/6/decisions/5/edit 处的 NoMethodError

#<#:0x007fd1ff569130> 的未定义方法“decision_path”

我正在使用'better_errors'gem,它引用了form_forno 方法错误的第一行:

<%= form_for(@decision) do |f| %>

我的代码中没有'decision_url'任何内容。我错过了什么?不应该form_for知道将更新放在哪里?它有一个有效的路径:

/groups/:group_id/decisions/:id(.:format)

以下是日志中看起来最令人兴奋的部分:

Rendered decisions/_form.html.erb (6.5ms)
  Rendered decisions/edit.html.erb within layouts/application (7.1ms)
Completed 500 Internal Server Error in 9ms (ActiveRecord: 0.1ms)

NoMethodError - undefined method `decision_path' for #<#<Class:0x007fd5d98527b0>:0x007fd5d400d618>:
  actionpack (4.2.3) lib/action_dispatch/routing/polymorphic_routes.rb:220:in `polymorphic_method'
  actionpack (4.2.3) lib/action_dispatch/routing/polymorphic_routes.rb:134:in `polymorphic_path'
  actionview (4.2.3) lib/action_view/helpers/form_helper.rb:466:in `apply_form_for_options!'
  actionview (4.2.3) lib/action_view/helpers/form_helper.rb:434:in `form_for'
4

2 回答 2

1

您已经像这样指定了嵌套资源:

resource groups do
  resource decisions
end

但是,应该使用符号指定路由,如下所示:

resource :groups do
  resource :decisions
end

确保您的模型也定义了正确的关系:

class Group < ActiveRecord::Base
  has_many :decisions
end

class Decision < ActiveRecord::Base
  belongs_to :group
end
于 2015-08-25T00:34:25.330 回答
0

这是decision_url它抱怨的电话:

if @decision.update(decision_params)
        format.html { redirect_to @decision), notice: 'Decision was successfully updated.' }

更改@decisiongroup_decision_path(@group, @decision)修复它。

于 2015-08-25T23:25:01.630 回答