has_one accepts_nested_attributes_for fields_for NOT WORKING HELP

372 views
Skip to first unread message

Humberto Ribeiro

unread,
Nov 28, 2010, 11:29:02 AM11/28/10
to Ruby on Rails: Talk

MODEL
class User < ActiveRecord::Base
has_one :address, :dependent => :destroy
accepts_nested_attributes_for :address
end

CONTROL
def new
@user = User.new
@user.build_address # Adicionei
...

VIEW partial _form
....
<% f.fields_for :address do |b| %> # Adicionei
<%= b.text_field :city_manual %> # Adicionei
<% end %> # Adicionei
....

So this is not working... when browser to users/new it do not show the
field as should...
Change this to a has_many all works.
Using has_one this make me crazy and not work... anyone help?

Walter Lee Davis

unread,
Nov 28, 2010, 11:41:50 AM11/28/10
to rubyonra...@googlegroups.com

Sorry, I don't have an answer for you on this. But I'm curious -- why
are you putting the address in a separate model if you are using
has_one? It seems like an over-normailization to me. Why not just put
those attributes in the user and be done with it?

Walter

Humberto Ribeiro

unread,
Nov 28, 2010, 11:50:58 AM11/28/10
to Ruby on Rails: Talk
Model address has 8 attributes... i did it to minimize user table
size.

About the problem... just try in your machine you will se it not
workin as should do.

ssmithstone

unread,
Nov 28, 2010, 12:51:01 PM11/28/10
to Ruby on Rails: Talk
in your

def new

try @user.address = Address.new

Humberto Ribeiro

unread,
Nov 29, 2010, 12:08:05 AM11/29/10
to Ruby on Rails: Talk
The result is the same... the field do not show in view when browsing
to /users/new

tundrax

unread,
Nov 29, 2010, 2:55:48 AM11/29/10
to Ruby on Rails: Talk
Just tested. Everything works fine.

Make sure:

1) you didn't forget to put "belongs_to :user" association in your
Address model
2) use "<%=" for your "f.fields_for :address" in the _form view file.

Hope this helps.

tundrax

unread,
Nov 29, 2010, 3:00:18 AM11/29/10
to Ruby on Rails: Talk
Reply all
Reply to author
Forward
0 new messages