2016-11-23 73 views
1

花了最後一天半的時間,通過SO這裏的精彩線索和在線閱讀其他東西,我仍然無法抓住解決方案以及這些移動部件如何一起工作。如何通過關聯創建一個刪除has_many的link_to刪除操作?

我通過CountyTownshipRangeSurvey獲得了County和TownshipRange之間的多對多關係。這種關係通過嵌套資源在路線文件中指出。

我希望用戶能夠刪除兩條記錄之間的關聯,但是我遇到的問題是TownshipRange索引頁上的刪除鏈接只接受township_range id值而不是關聯的縣id值。請注意,在查看索引頁(其中包含刪除鏈接)時,顯示正確的縣id參數。

我該如何允許?

我的直覺是我需要在兩個地方做出改變。首先,路由文件,因此它會接受縣編號,第二,到視圖中的link_to。

作爲一個後續問題......我意識到我試圖將功能放入township_ranges_controller。這是問題的一部分嗎?我能否更好地將這一功能轉移到專門用於創建「直通表」關聯的單獨控制器中?

謝謝大家的寶貴見解!如果您需要更多代碼片段,請告訴我!

模型county.rb

class County < ApplicationRecord 
    has_many :township_ranges, through: :county_township_range_surveys 
    has_many :county_township_range_surveys 
    ... 
end 

模型township_range.rb

class TownshipRange < ApplicationRecord 
    has_many :counties, through: :county_township_range_surveys 
    has_many :county_township_range_surveys 
    ... 
end 

模型county_township_range_survey.rb

class CountyTownshipRangeSurvey < ApplicationRecord 
    belongs_to :county 
    belongs_to :township_range 
    ... 
end 

控制器township_ranges_controller.rb

routes.rb中的

相關部分(擴展20161124)

resources :states, shallow: true do 
    resources :counties do 
    resources :township_ranges do 
     resources :sections 
    end 
    end 
end 

導軌路由指示產生的路線不查找相關聯的縣/:ID爲刪除操作。

township_ranges GET /counties/:id/township_ranges(.:format)  township_ranges#index 
        POST /counties/:id/township_ranges(.:format)  township_ranges#create 
new_township_range GET /counties/:id/township_ranges/new(.:format) township_ranges#new 
edit_township_range GET /township_ranges/:id/edit(.:format)   township_ranges#edit 
    township_range GET /township_ranges/:id(.:format)    township_ranges#show 
        PATCH /township_ranges/:id(.:format)    township_ranges#update 
        PUT /township_ranges/:id(.:format)    township_ranges#update 
        DELETE /township_ranges/:id(.:format)    township_ranges#destroy 

它會尋找完整的路線DELETE counties/:county_id/township_range/:id/如果我本身隔離嵌套這樣

resources :counties do 
    resources :township_ranges 
end 

但是,保持縣被嵌套在狀態,這不是我後。 ..

township_range/index.html.erb

<td><%= link_to 'Destroy', township_range, method: :delete, 
      data: { confirm: "Delete #{township_range.township} 
      #{township_range.range} ?" } %></td> 

發展。登錄(與鏈接更新由@Wish區的要求)

Started GET "/counties/25/township_ranges" for 127.0.0.1 at 2016-11-23 15:23:20 -0600 
Processing by TownshipRangesController#index as HTML 
    Parameters: {"id"=>"25"} 
    [1m[36mCounty Load (0.1ms)[0m [1m[34mSELECT "counties".* FROM "counties" WHERE "counties"."id" = ? LIMIT ?[0m [["id", 25], ["LIMIT", 1]] 
    Rendering township_ranges/index.html.erb within layouts/application 
    [1m[36mTownshipRange Load (34.5ms)[0m [1m[34mSELECT "township_ranges".* FROM "township_ranges" INNER JOIN "county_township_range_surveys" ON "township_ranges"."id" = "county_township_range_surveys"."township_range_id" WHERE "county_township_range_surveys"."county_id" = ?[0m [["county_id", 25]] 
    Rendered township_ranges/index.html.erb within layouts/application (72.6ms) 
Completed 500 Internal Server Error in 113ms (ActiveRecord: 35.2ms) 



ActionView::Template::Error (No route matches {:action=>"show", :controller=>"township_ranges", :county_id=>#<County id: 25, name: "comanche", abbreviation: nil, state_id: 35, created_at: "2016-11-22 16:24:52", updated_at: "2016-11-22 16:24:52">, :id=>nil} missing required keys: [:id]): 
    22:   <td><%= link_to 'Edit', edit_township_range_path(township_range) %></td> 
    23:   <%# QUESTION: Do I need to modify the link here so it somehow takes in the county id param? %> 
    24:   <%# NOTE: This will likely also require some sort of customization to the routes file so that the county_id is passed as a param %> 
    25:   <td><%= link_to 'Destroy', township_range_path(id: @township_range, county_id: @county), method: :delete, 
    26:     data: { confirm: "Delete #{township_range.township} #{township_range.range} ?" } %></td> 
    27:  </tr> 
    28:  <% end %> 
+0

路線應該是township_range(ID:township_range,COUNTRY_ID :country)和控制器中可以找到Country.find(params [:country_id]) –

回答

2

根據您的情況路線應該是

township_range_path(id: @township_range , county_id: @county) 

和控制器,你可以找到

County.find(params[:county_id]) 

添加人Spectator6:最終的答案最終是刪除我原來在我的路線中的成員。工作刪除路徑結束了

township_range_path(id: township_range, county_id: @county) 

再次感謝@Wish區的耐心和幫助我瞭解的link_to路徑和路線好一點:)

+0

好的,謝謝。我如何實現路線文件?我假設以某種方式覆蓋資源:township_ranges?需要那邊 – Spectator6

+1

@ Spectator6沒有改變你的要求會以這種方式 去:刪除 /township_ranges/9 COUNTRY_ID = 12 –

+0

@ Spectator6 –

相關問題