如何创建link_to删除通过关联删除has_many的操作?

时间:2016-11-23 17:09:29

标签: ruby-on-rails ruby-on-rails-5

花了最后一天半在这里搜索SO的精彩线索,并在线阅读其他内容,我仍然难以掌握解决方案以及这些移动部件如何一起运作。

我和县乡之间有多对多的关系,通过郡乡镇的范围调查。此关系在路径文件中通过嵌套资源指示。

我希望用户能够删除两个记录之间的关联,但我遇到的问题是TownshipRange索引页面上的删除链接只接受township_range id值而不是相关的县id值。请注意,查看索引页面(包含删除链接)时,会出现正确的县id参数。

我该如何允许?

我的预感是我需要在两个地方做出改变。首先,对于routes文件,它将接受县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

model township_range.rb

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

model county_township_range_survey.rb

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

Controller township_ranges_controller.rb

before_action :set_township_range, only: [:show, :edit, :update, :destroy]

...

# QUESTION: How do I find the appropriate county?
def destroy
  @county = County.find(params[:id]) # This does not work
  @county.township_ranges.destroy(@township_range)
  redirect_to township_ranges_path(@county)
  flash[:success] = "Township and Range has been deleted"
end

....

private

    def set_township_range
      @township_range = TownshipRange.find(params[:id])
    end

    ...

routes.rb的相关部分(扩展20161124)

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

Rails路由表示生成的路由不会查找DELETE操作的关联县/: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>

development.log(根据@Wish Zone的要求更新链接)

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 %>

1 个答案:

答案 0 :(得分: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 Zone的耐心和帮助我理解link_to路径并更好地路由:)