2013-01-09 11 views
1

두 모델이 있습니다. 작업 및 위치has_one 관계 컨트롤러 업데이트 레일 3

각 작업 has_one 위치

task.rb

has_one :location 

나는 위치 모델 내 컨트롤러를 만들려고하고 있습니다. 이 현재 모든 기능이 업데이트를 제외하고 제대로 작동 새로운 형태

<%= form_for(@task.build_Location, :url => task_Location_path(@task)) do |f| %> 
     Fields 
     <%= f.submit %> 
    <% end %> 

및 편집 양식

<%= form_for(@task.Location, :url => task_Location_path(@task)) do |f| %> 
     Fields 
     <%= f.submit %> 
    <% end %> 

입니다.

def create 
    @location = @task.create_Location(params[:location]) 
end 
def update 
    @location = @task.locations.find(params[:id]) 
end 

이 방법을 정의하는 적절한 방법은 무엇입니까?

+1

'을 (PARAMS는 [: 위치])'난 그냥 – MrYoshiji

답변

1
def update 
    @location = @task.location.update_attributes(params[:location]) 
end 
2

회원님이

당신이 보통이 ID의를 통해 @task에 액세스하려면 ... 일을하려고하지만 무엇을 정확히 확인; 또한 has_one이라면 복수화되지 않을 것입니다. 당신이 '뭔가'에 작업의 이름을 업데이트하려고 한 경우

당신은 같은 것을 할 수있는 : location.update_attributes @

def update 
    @location=Location.find(params[:id]) 
    @location.task.name="something" 
    @location.save 
end 
+0

에게 일할 수있는 폼에 지정된 특성을 제대로 업데이트하려면 업데이트 메서드를 사용하십시오. – capcode01

+0

당신은 capcode 's와 같은 것을 원할 것입니다; 또한 attr_accessible보세요 - tasks_attributes와 같은 것이 필요합니다 – timpone

+0

도와 주셔서 감사합니다. 올바른 방향으로 지적 해 주셨습니다. 위의 해결책은 마침내 나를 위해 일한 것입니다. 감사합니다 – capcode01