2011-02-15 5 views
1

사용자가 작업을 갖고 작업에 위치가있는 앱을 만들고 있습니다. 작업 및 위치는 formtastic_cocoon을 사용하여 nested_form 형식으로되어 있습니다. formtastic_cocoon은 jQuery 확장자가있는 formtastic gem입니다.레일스 형식으로 연결 값 가져 오기

location.address 필드는 이미 데이터베이스에있는 주소를 검색하는 자동 완성 텍스트 필드입니다. 따라서 사용자가 주소를 선택하면 양식의 숨겨진 location_id가 채워집니다.

사용자가 작업을 편집하려고 할 때 현재 선택한 주소를 표시하려고하지만 그 값을 검색 할 수있는 곳이 어디에도 표시되지 않습니다. location_id를 가져올 수 있습니다. 작업 모델에 포함되어 있지만 관련 location.address를 가져올 수 없습니다. -----

모델은 내 양식에서 다음

 
class Task < ActiveRecord::Base 
    attr_accessible :user_id, :date, :description, :location_id 

    belongs_to :user 
    has_one :location 
end 

class Location < ActiveRecord::Base 
    attr_accessible :address, :city, :state, :zip 

    has_many :tasks 
end 

있습니다 나는 모든 formtastic 코드를 포함하는 편집 -----------

 
    <div class="nested-fields"> 
    <%= link_to_remove_association "remove task", f %> 
     <div class="searchAddress"> 
     < input type="text" value="HERE IS WHERE I WANT TO SHOW THE ADDRESS" > 
    </div> 
    <%= f.hidden_field :location_id %> 
    <%= f.inputs :date, description %> 
</div> 

이 ----------

 
form.html.erb 
<%= semantic_form_for @user, :html=>{:multipart=true} do |form| %> 

    <%= form.inputs :username, :photo %> 


     <%= form.semantic_fields_for :tasks do |builder | %> 

    <%= render 'task_fields', :f => builder %> 
    <% end %> 
<% end %> 

------- 최종 편집 -------------- 나는 'F의 다른 방식을하고 출력하기 시도 ',하지만 보이지 않는 y 관련 위치에 대한 참조, 아직 양식 밖으로 User.Task [0] .Location 디버깅 할 경우 올바른 위치 정보를 가져옵니다. 어떻게하면 그걸 폼 안에 넣을 수 있을까요 ??

--------- 업데이트 ------------

이 부분에 좀 더 가까워지고 있습니다. 출력 할 수 있습니다.

 
<%= debug f.object %> 

반환되는 작업 개체를 가져옵니다. 불행히도 그것은 위치 객체를 포함하지 않고 location_id 필드의 값만을 포함합니다.

+0

당신은'<% = form_for @task do | f | %>'맞죠? – LapinLove404

+0

비슷합니다, 나는 더 Semantic_fields_for에있어 Form_for보다 더 field_for와 비슷합니다 – pedalpete

+0

Formtastic을 사용하고 있습니까? – LapinLove404

답변

0

@ task.location.address를 사용해 보셨습니까?

+0

나는 그것을 시도했다, 나는 '위치'에 대해 정의되지 않은 메소드를 얻는다. : Nilclass – pedalpete

+0

@task가 nil이 아니 겠는가? – LapinLove404

+0

@ user.tasks는 nil이 아니며 @ user.task는 nil입니다. @ user.tasks.location은 nil입니다. – pedalpete