SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

Nested Has_one Relationship with Fields_for and Attr_accessible in Model Class

       

To make child attributes accessible to your model through a nested forms (Rails 2.3) you'll need to add the "#{child_class}_attributes" to the attr_accessible method in your parent class. If you don't use attr_accessible in your parent model (you would do this to restrict certain attributes to be accessed via a web form) then you should be all set.

Below is an example where User has_one Profile with the favorite_color attribute being set/updated in the nested form.


class User < ActiveRecord::Base
  has_one :profile #child class
  accepts_nested_attributes_for :profile
  attr_accessible :profile_attributes # the format is the child_class followed by the "_attributes"
end

And the form would like this...


<% form_for @current_user do |f| %>
   <% f.fields_for :profile do |profile| %>
     <%= profile.text_field :favorite_color %>
  <% end %>
<% end %>

Tagged w/ #attr_accessible #has_one #nested formsruby on rails

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app