I m building an app where a User has tasks and a task has a location. The tasks and locations are in a nested_form using formtastic_cocoon, which is the formtastic gem with a jQuery extension.
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
那么以我的形式,我有
<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>
-----------经过编辑以包含所有formtastic代码---------------
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 %>
------- end edit -------------- I have tried outputing different manner of f , but don t see any reference to the associated locations, yet if I debug User.Task[0].Location outside of the form, I get the correct location details. How do I get that inside the form??
---------更新------------
离这件事越来越近了。原来我可以输出
<%= debug f.object %>
我得到了返回的任务对象。不幸的是,它不包括location对象,只包括location_id字段的值。