2011-09-14 6 views
2

誤ったコントローラーの削除に対するbutton_toルーティングに問題があります。目的は、割り当てコントローラでdeleteメソッドを呼び出して関係を削除し、要求またはロケーションを削除しないことです。問題は、ロケーションコントローラにルーティングを継続することです。私が主張する場所のすべてを取得するには、次の文を持って主張コントローラ内Rails 3 - 間違ったコントローラーメソッドへのButton_toのルーティング(:delete)

Claim 
has_many :assignments 
has_many :locations, :through => :assignments 

Assignment 
belongs_to :claim 
belongs_to :location 

Location 
has_many :assignments 
has_many :claims, :through => :assignments 

私はHMTのセットアップと三つのモデルを持っています。特許請求の範囲に

@locations = @claim.locations.all 

私はボタンを選択すると、次の文

<% @locations.each do |location| %> 
... 
<td><%= button_to 'Remove', location , :method => :delete %></td> 
<% end %> 

だから、それは場所のコントローラ内deleteメソッドを呼び出してい表示します。 クレームと場所の間のリンクである割り当てコントローラ内のdeleteメソッドを呼び出すように設定する必要があります。

  1. 私は@locations = @ claim.locations.allも使用して割り当て情報を読み取るためのデータを取得する方法を変更しようとしています。include、または:参加するが、何もデータが返さに追加するようです。

  2. 私は割り当てを呼び出すためにbutton_toを変更しようとしましたが、私はどのようにわかりません。

答えて

0

あなたは多分代わりの場所、割り当てを反復処理できます。あなたのビューで

@assignments = @claim.assignments.include(:locations).all 

を、あなたは位置情報を表示することができます。

<%= @assignments.each do |a| %> 
    <h3><%= a.location.name %></h3> 
    ...etc 
    <%= button_to 'Remove', a , :method => :delete %> 
<% end %> 
0

あなたの最善の策は、別のものを追加することですその行動への行動と経路。

def remove 
    # You'll have to pass the current claim.id in via the form 
    my_claim = Claims.find(params[:claim_id]) 
    @location.claims.delete(my_claim) 
end 

は実際にこれを試していないが、それは最小限の労力で動作するはずです:あなたcontrollers/location_controller.rbプットに続いて

match 'location/remove' => 'location#remove', :via => [:delete] 

:だからあなたのconfig/routes.rbファイル内のようなものを追加します。

Credit for the delete trick

関連する問題