Hello,
I am hoping one of you Rails gurus can help a poor newb.
I have a User model and a Contact model. The user Model
has_one :contact. I would like to use the users/new view to create a
new user as well as the related contact. However I can''t figure out
how to add the appropriate text_fields to the form to about info for
the related contact. Can anyone point me in the right direction?
Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
At your controller:
class UsersController < ApplicationController
def new
@user = User.new
@contact = @user.build_contact
end
def create
@user = User.new( params[:user] )
@contact = @user.build_contact( params[:contact] )
if @user.valid? and @contact.valid?
@user.save
@contact.save
respond_to do |format|
format.html do
flash[:notice] = "User saved"
redirect_to users_path
end
end
else
render :action => ''new''
end
end
end
At your new.html.erb
<% form_for @user do |f| %>
Login: <%= f.text_field :login %> <br/>
Email: <%= f.text_field :email %> <br/>
<% fields_for :contact do |g| %>
Name: <%= text_field :email %>
Address: <%= text_area :address %>
<% end %>
<% end %>
-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en)
On Sat, Dec 20, 2008 at 9:08 PM, idflyfish
<idflyfish-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> Hello,
>
> I am hoping one of you Rails gurus can help a poor newb.
>
> I have a User model and a Contact model. The user Model
> has_one :contact. I would like to use the users/new view to create a
> new user as well as the related contact. However I can''t figure
out
> how to add the appropriate text_fields to the form to about info for
> the related contact. Can anyone point me in the right direction?
>
> Thanks!
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On Dec 20, 11:50 pm, "Maurício Linhares" <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: [...]> At your new.html.erb > > <% form_for @user do |f| %> > > Login: <%= f.text_field :login %> <br/> > Email: <%= f.text_field :email %> <br/> > > <% fields_for :contact do |g| %> > Name: <%= text_field :email %> > Address: <%= text_area :address %> > <% end %>Um, those last two should be g.text_field .> > <% end %> >See http://www.stephenchu.com/2008/03/boost-your-controller-paramsfu.html for an excellent series of blog posts detailing this and other similar tricks. Best, -- Marnen Laibow-Koser marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org http://www.marnen.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---