2011-09-14 3 views
2

틀린 컨트롤러 삭제로의 button_to 라우팅 문제가 있습니다. 목표는 할당 컨트롤러에서 delete 메소드를 호출하고 관계를 삭제하지만 청구 또는 위치를 삭제하지 않는 것입니다. 문제는 위치 컨트롤러로 라우팅을 유지한다는 것입니다. 내가 주장의 위치를 ​​모두 얻을 수있는 다음과 같은 문이 주장 컨트롤러 내에서레일 3 - 잘못된 컨트롤러 방법으로의 Button_to 라우팅 (: 삭제)

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

I 버튼을 선택하면 그래서이 위치 컨트롤러에서 삭제 메소드를 호출을 볼 수 있습니다. 클레임과 위치 사이의 링크 인 할당 컨트롤러 내에서 delete 메소드를 호출하도록 설정해야합니다. 포함 시키거나 :

  1. 나는 데이터 @locations = @ claim.locations.all 또한 사용하여 할당 정보를 읽을 수 방법을 변경하는 시도에 가입하지만 아무것도 반환 된 데이터에 추가 보인다.

  2. 나는 과제를 호출하기 위해 button_to를 변경하려했지만 어떻게해야할지 모르겠다.

답변

0

당신은 아마 대신 위치, 할당을 반복 수 :

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

가장 좋은 방법은 다른를 추가하는 것입니다

보기에
@assignments = @claim.assignments.include(:locations).all 

, 당신은 위치 정보를 표시 할 수 있습니다 그 행동에 대한 행동과 경로.

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