2011-09-14 103 views
2

我有問題的button_to路由到錯誤的控制器刪除。目標是調用分配控制器中的刪除方法並刪除關係,但不刪除聲明或位置。問題是它保持路由到位置控制器。Rails 3 - Button_to路由到錯誤的控制器方法(:刪除)

我有三個型號有HMT設置:

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 

對這些索賠控制器我有以下語句獲取所有位置的要求。

@locations = @claim.locations.all 

在理賠查看我有以下聲明

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

所以,當我選擇按鈕,它調用位置控制器內刪除方法。 我需要將它設置爲在分配控制器中調用聲明和位置之間鏈接的刪除方法。

  1. 我試圖改變我如何獲取數據@locations = @ claim.locations.all也閱讀使用分配信息:包括,或:加入,但似乎沒有將其添加到返回的數據。

  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

最好的辦法是增加另一個行動和途徑。因此,在您config/routes.rb文件中添加類似:

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

然後在您的controllers/location_controller.rb地說:

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 

實際上還沒有試過,但它應該以最小的努力工作。

Credit for the delete trick