Rails,销毁链接NoMethodError

时间:2014-07-23 09:13:00

标签: ruby-on-rails ruby-on-rails-3

我有以下项目清单:

= @kid.educations.each do |education|
            = education.studies_centre
            = _('-')
            = education.city_and_country
            = link_to _("<i class='fa fa-times-circle a-lg'></i> delete").html_safe, education, :confirm => 'Are you sure?',:method => :delete, class: "btn btn-danger btn-xs pull-right"
            %br
            %small
              = education.academic_qualification
            %hr
            %br

正如您所看到的,我有一个删除选项,但我没有工作。我明白了:

undefined method `education_path' for #<#<Class:0x007fd00c61c270>:0x007fd00cdd5b88>

我做错了什么。

感谢您的帮助

更新路线


                dashboard_kid_educations GET      /dashboard/kids/:kid_id/educations(.:format)                           dashboard/educations#index
                                              POST     /dashboard/kids/:kid_id/educations(.:format)                           dashboard/educations#create
                  new_dashboard_kid_education GET      /dashboard/kids/:kid_id/educations/new(.:format)                       dashboard/educations#new
                 edit_dashboard_kid_education GET      /dashboard/kids/:kid_id/educations/:id/edit(.:format)                  dashboard/educations#edit
                      dashboard_kid_education GET      /dashboard/kids/:kid_id/educations/:id(.:format)                       dashboard/educations#show
                                              PUT      /dashboard/kids/:kid_id/educations/:id(.:format)                       dashboard/educations#update
                                              DELETE   /dashboard/kids/:kid_id/educations/:id(.:format)                       dashboard/educations#destroy

3 个答案:

答案 0 :(得分:1)

您正在使用命名空间的嵌套资源,因此您的链接应如下所示:

 = link_to _("<i class='fa fa-times-circle a-lg'></i> delete").html_safe, [:dashboard, @kid, education], :confirm => 'Are you sure?',:method => :delete, class: "btn btn-danger btn-xs pull-right"

答案 1 :(得分:1)

如果使用嵌套资源,则应将2个参数传递给路径助手。我认为使用块来提高代码可读性更好

= link_to dashboard_kid_education_path(@kid, education), confirm: 'Are you sure?', method: :delete, class: 'btn btn-danger btn-xs pull-right' do
  %i.fa.fa-times-circle.a-lg
  = 'delete'

答案 2 :(得分:0)

嵌套路线

要添加到Marek的答案是nested routes

的想法

基本上,当你定义一个嵌套的路由时(如下所示),Rails并不会本能地知道你什么时候调用它:

#config/routes.rb
resources :dashboard do
    resources :education
end

最重要的是,Rails只会调用路由,因为它们与您正在呼叫的对象的model相关。在您的示例中,您调用education(可能是从Eduction.find()构建的)

对于Rails,通过将其传递给routes,您实际上会告诉您的应用程序仅查找Education模型的路由,因此您的错误看起来像这样:

undefined method `education_path'

-

Marek详细说明解决此问题的方法是确保您在<%= link_to %>电话中引用正确的路径。为此,您必须引用Marek的解决方案

所描述的嵌套路由。