2016-11-23 249 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 –