2016-11-23 3 views
1

最後の1日半は、ここの偉大なスレッドを精読し、他のものをオンラインで読むのに費やしました。関連を介してhas_manyを削除するアクションを削除するlink_toを作成するにはどうすればよいですか?

私はCountyTownshipRangeSurveyからCountyとTownshipRangeの間に多対多の関係があります。この関係は、ネストされたリソースを介してルートファイルに示されます。

2つのレコード間の関連付けを削除できるようにしたいが、私が実行している問題は、TownshipRangeインデックスページの削除リンクのみが、関連するcounty id値ではなく、township_range id値を受け入れることです。インデックスページ(削除リンクを含む)を表示しているときに、正しいcounty idパラメータが存在することに注意してください。

どうすればよいですか?

私は、2つの場所で変更を加える必要があることを知っています。まず、routesファイルに、county idとsecondを受け付けるように、ビューの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 

モデルモデルcounty_township_range_survey.rb

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

township_range.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 

レール経路関連

は、生成された経路は、関連郡/探しない示す: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 

私はこの

resources :counties do 
    resources :township_ranges 
end 

のように自身でネストを隔離するが、それは私が後だものではありませんこれは、状態の下にネストされてからの郡を続けるかどうかは、完全なルートDELETE counties/:county_id/township_range/:id/を探します。 ..

township_range/index.html.erb

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

の開発。あなたの状況によると、ルートは

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

べきであり、コントローラに、あなたはSpectator6に追加

County.find(params[:county_id]) 

を見つけることができますログ

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でなければなりません:country)、コントローラではCountry.find(params [:country_id])を見つけることができます –

答えて

2

(@Wishゾーンによって要求されたリンク更新で):最終的な答えは、私のルートにもともと持っていたメンバーのメンバーを削除することでした。 township_range、COUNTRY_ID:ワーキング削除パスはあなたの忍耐のために、私は少し良くのlink_toパスとルートを理解する手助けのため

township_range_path(id: township_range, county_id: @county) 

おかげで再び@Wishゾーン:)

+0

よろしくお願いいたします。ルートファイルを実装するにはどうすればよいですか?私はリソースをオーバーライドすると仮定します:township_ranges何らかの方法で? Spectator6 @ – Spectator6

+1

あなたの要求は、このよう に行きますSpectator6 @ –

+0

あそこに必要な変更なし:?大丈夫ああ /township_ranges/9 COUNTRY_ID = 12 –

関連する問題