Hello:

I am quite new to Ruby and Rails. Here are the details of a sample use
case that I am trying to build.

Development environment: (on Ubuntu 9.04)
##################################
Ruby version              1.8.7 (i486-linux)
RubyGems version          1.3.1
Rails version             2.1.0
Active Record version     2.1.0
Action Pack version       2.1.0
Active Resource version   2.1.0
Action Mailer version     2.1.0
Active Support version    2.1.0
Edge Rails revision       unknown
Application root          /home/vkottilil/myapp
Environment               development
Database adapter          sqlite3
Database schema version   20091120203804

Sample Application:
################
User and Address maintenance - a bunch of user data with addresses

Model:
######
class User < ActiveRecord::Base
  has_one :address
  validates_presence_of :first_name, :last_name
 end
class Address < ActiveRecord::Base
  belongs_to :user
end

Controller:
########
users_controller.rb (new and create blocks)

 def new
    @user = User.new
    @address = Address.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user }
    end
  end

def create
    @user = User.new(params[:user])  ## --> line 45
    @address = @user.address.build(params[:address])
    respond_to do |format|
      if @user.save
          flash[:notice] = 'User was successfully created.'
          format.html { redirect_to(@user) }
          format.xml  { render :xml => @user, :status
=> :created, :location => @user }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @user.errors, :status
=> :unprocessable_entity }
      end
   end
end


The view comes up with out errors (http://localhost:3000/users/new)
and when I click on create this error is generated.

ActiveRecord::AssociationTypeMismatch in UsersController#create
Address(#-615658568) expected, got HashWithIndifferentAccess
(#-606879698)
RAILS_ROOT: /home/vkottilil/myapp

stack dump ---
-----
app/controllers/users_controller.rb:45:in `new'
app/controllers/users_controller.rb:45:in `create'

----
-------

Request Parameters:

{"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.

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