get association values in rails forms
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.
The location.address field is an autocomplete text field searching on addresses that already exist in the database. So when the user selects the address, a hidden location_id in the form is populated.
What I am trying to do is that when the user goes to edit the task, I want it to display the currently selected address, but I don't see anywhere that I can retrieve that value. I can get the location_id, as that is in the task model, but I can't seem to get the associated location.address.
the models are
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
then in my form, I have
<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>
----------- edited to include all formtastic code ---------------
form.html.erb <%= semantic_form_for @user, :html=>{:multipart=true} do |form| %> <%= form.inputs :username, :photo %开发者_JS百科> <%= 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??
--------- update ------------
getting a bit closer on this. It turns out I can output
<%= debug f.object %>
I get the task object returned. Unfortunately it does not include the location object, just the value of the location_id field.
Did you try @task.location.address ?
Your approach is right. But there is some slide miss-conception declaring association. I'ld like to change as below:
# app/models/task.rb
class Task < ActiveRecord::Base
attr_accessible :user_id, :location_id, :date, :description
belongs_to :user
belongs_to :location
end
As we are storing location
foreign key in the tasks
table, it's better to declare belongs_to
association here.
# app/models/location.rb
class Location < ActiveRecord::Base
attr_accessible :address, :city, :state, :zip
has_many :tasks
# this method will return full address of a location
def full_address
[address, city, state, zip].reject(&:blank?).join(", ")
end
end
Then you need to add the full_address
of a selected location.
# app/views/users/_task_fields.html.erb
<div class="nested-fields">
<%= link_to_remove_association "remove task", f %>
<div class="searchAddress">
<input type="text" value="<%= f.object.location&.full_address %>">
</div>
<%= f.hidden_field :location_id %>
<%= f.inputs :date, description %>
</div>
精彩评论