Nested forms validation problem

Hi All,

I’m trying to learn Ruby on Rails and have got an issue with the
validation of a nested form. I think I have got it setup correctly as it
appears to be saving the data as expected, but it is not wrapping the
CSS class (field_with_errors) to the fields, and if data is entered into
the nested fields it disappears on form submit (if there are validation
errors). The validation messages are being displayed correctly.

Here are the relevant bits of code (some of the forms have been
ommitted) - I am hoping somebody more experienced will be able to spot
where I have gone wrong!


class User < ActiveRecord::Base
has_one :feature
accepts_nested_attributes_for :feature


class Feature < ActiveRecord::Base
belongs_to :user
validates :profile, :hobbies, :presence => true


<% @user.build_feature %>
<%= form_for @user, :url => { :controller => :users, :action => “create”
} do |user| %>

<%= user.text_field :email %>

<%= render :partial => ‘features/form’,
:locals => { :form => user } %>


<%= form.fields_for :feature do |f| %>
<%= f.text_area :profile %>

Debugging @user.errors shows the subform items are being outputted as

:“feature.profile”: can’t be blank

Wheras the other form shows the error messages being output:

:first_name: can’t be blank

I’ve googled this issue but haven’t found anything that has worked -
would appreciate any help!


OK so I have had a bit of a breakthrough - the form works great when the
association is changed to has_many rather than has_one

What do I need to change to get this working for a has_one association?

Thanks in advance!

well what the output params look like? you have tu take the feature
from params and add it to user.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs