I have a user who has a location that location contains a country_id.
Location has a polymorphic interface locatable.
What works:
The user can edit their name, country and city. This all saves to the DB
on the update put.
The problem:
Returning to the edit page shows the textfield''s name and city
populated
but the country select form element defaults to blank. The value for
user.location.country_id is saved in the DB but the form element refuses
to show this as "selected". I have attempted explicityly setting the
selected :option but still, it fails to show country.
MODEL
user
has_one :location, :as => :locatable, :dependent => :destroy
location
belongs_to :country
belongs_to :locatable, :polymorphic => true
country
has_many :locations
VIEW
<% form_for @user, :html => { :multipart => true } do |form| %>
<div class="text_field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<% fields_for "user[location_attributes]",@user.location do
|location_fields| %>
<div class="select_field">
<%= location_fields.label :country_id %>
<%= location_fields.select :country_id, Country.find(:all).map { |c|
[c.name.titleize, c.id] }, { :include_blank => true } %>
</div>
<div class="text_field">
<%= location_fields.label :city %>
<%= location_fields.text_field :city %>
</div>
<%- end -%>
<%= form.submit ''Update'' %>
<% end %>
CONTROLLER
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
@user.attributes = params[:user]
@user.save!
flash[:notice] = "Your profile has been updated."
redirect_to url_after_update
rescue ActiveRecord::RecordInvalid
render :action => "edit"
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I forgot to mention. I adjusted the fields_for name to "user[location]" in case "_attributes" was causing the problem. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You may want to consider adding attr_accessible for the editable fields in your model. /dkm On Jan 14, 5:11 pm, Adam madd02 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I forgot to mention. > I adjusted the fields_for name to "user[location]" in case > "_attributes" was causing the problem. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for your reply DKM, I have location_attributes listed in the users attr_accessable list and use attr_protected for the :locatable_id and :locatable_type in location. Just to check that there are no attribute access issues i commented out attr_accessible and attr_protected''s in the user and location models. It still did not set the select. I believe it''s soemthing to do with the select name generated from the fields_for. <select name="user[location_attributes][country_id]" id="user_location_attributes_country_id"> . . but im not sure what. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---