Hi Fred,

Thank you so much for the quick response. It was quite helpful.  I
really wanted to test this out with out upgrading to the newer
version. I made the changes to view/params
submittal and the controller code and now it seems to be working. I
can see two saved records after a create event.

new.html.erb:

<% form_for( @user,
             :url => {  :action => 'create',
                        :first_name => @user.first_name,
                        :last_name => @user.last_name,
                        :home_phone => @user.home_phone,
                        :work_phone => @user.work_phone,
                        :cell_phone => @user.cell_phone,
                        :email => @user.email,
                        :profile_name => @user.profile_name,
                        :active => @user.active,
                        :address1 => @address.address1,
                        :address2 => @address.address2,
                        :city => @address.city,
                        :state => @address.state,
                        :country => @address.country
                    }
          ) do | f |
%>

----- rest of the form fields ---

In the users_controller.rb, removed the previous build invocation and
put these lines of code to manually set the FK for the association.

def create

    Rails.logger.debug(params)
    @user = User.new
    @address = Address.new

    @user.first_name = params[:user][:first_name]
    @user.last_name = params[:user][:last_name]
    @user.profile_name = params[:user][:profile_name]
    @user.home_phone = params[:user][:home_phone]
    @user.work_phone = params[:user][:work_phone]
    @user.cell_phone = params[:user][:cell_phone]
    @user.email = params[:user][:email]
    @user.active = params[:user][:active]

    @address.address1 = params[:user][:address][:address1]
    @address.address2 = params[:user][:address][:address2]
    @address.city = params[:user][:address][:city]
    @address.state = params[:user][:address][:state]
    @address.zip = params[:user][:address][:zip]
    @address.country = params[:user][:address][:country]

    respond_to do |format|

        if @user.save
          @address.user_id = @user.id
          if @address.save
            flash[:notice] = 'User was successfully created.'


Thanks again for the excellent comments,

best regards
Vasu

Vasu Kottilil


On Dec 4, 11:09 am, Frederick Cheung <[email protected]>
wrote:
> On Dec 4, 6:58 pm, Vasu Kottilil <[email protected]> wrote:
>
>
>
>
>
> > {"commit"=>"Create",
> >  "authenticity_token"=>"1459525ad4fd5ae39be0c011edc5c45fdbff4337",
> >  "user"=>{"work_phone"=>"",
> >  "address"=>{"address1"=>"ad1",
> >  "city"=>"any town",
> >  "address2"=>"line2",
> >  "zip"=>"12345",
> >  "country"=>"USA",
> >  "state"=>"CA"},
> >  "profile_name"=>"pfname",
> >  "home_phone"=>"",
> >  "cell_phone"=>"",
> >  "last_name"=>"last",
> >  "first_name"=>"first",
> >  "email"=>"",
> >  "active"=>"1"}}
>
> > It is a simple 1-1 relationship - each user has one address. In this
> > use case I am trying to create data in two tables using a single
> > form.
>
> your code looks like you expect params[:address] to be set, but it
> isn't (instead params[:user][:address] is set).
> Rails is then trying to do user.address = params[:user][:address]
> which doesn't work because you're trying to stuff a hash into an
> association. It can be made to work with the nested attributes stuff
> but that's only in rails 2.3. You could change your views to not
> submit parameters in this way or read up on nested attributes.
>
> Fred
>
>
>
> > I am certain that I am missing some more code that is required to
> > successfully save. Any clues greatly appreciated. Any suggestions to
> > implement in a different way are welcome.  Can I explicitly write ruby
> > code to insert records into these tables even though I created the
> > objects using the rails scaffolding?
>
> > Thanks much
>
> > Vasu Kottilil

--

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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to