2014-07-23 74 views
1

我有一個項目下面的列表:軌,破壞鏈接NoMethodError

= @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 
+0

顯示您的'routes.rb'或輸出'rake routes'。 –

+0

準備就緒的路線。 – Jean

回答

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" 
+0

感謝您的回答,它的工作原理是唯一的問題,如果它向我展示「Are you sure?alert 3 times。 – Jean

+0

@Jean是否有效? –

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

嵌套路線

東西添加到Marek「的答案是:nested routes

理念從本質上講,當你定義一個嵌套路由(如下所示),Rails不會本能地知道你什麼時候調用它:

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

底線是Rails只會調用路由,因爲它們與您要呼叫的對象的model有關。在你的榜樣,你叫education(這大概是從​​建)

到Rails,通過傳遞給你的routes,你會基本上是告訴你的應用程序以查找Education模型而已,因此爲什麼你的錯誤是這樣的路線這樣的:

undefined method `education_path' 

-

解決這個問題的辦法,由Marek詳細的是,以確保您引用正確的路徑在您的通話<%= link_to %>。要做到這一點,你必須參照馬雷克解決方案描述的嵌套路線