I''m in the process of creating a sign up form for an online
application.
The form collects account info, company info, and then info for an
administrative user.
The method looks like this:
def create
@account = Account.create!(params[:account])
@company = @account.companies.create!(params[:company])
@user = @company.users.create!(params[:user])
end
However, this inevitably fails on validation and ActionController comes back
saying that "Validation failed: Username can''t be blank, First
name can''t be
blank, Last name can''t be blank, Email address can''t be
blank"
However, values for user are present in the request parameters:
*Parameters*: {"company"=>{"name"=>"poop",
"account_id"=>29},
"user"=>{"password_confirmation"=>"poop",
"username"=>"poop",
"company_id"=>29, "time_zone"=>"International
Date Line West",
"first_name"=>"poop",
"password"=>"poop",
"last_name"=>"poop",
"email_address"=>"poop"},
"account"=>{"name"=>"poop",
"url"=>"poop"}}
I''ve tried using User.new(params[:user]) with the same results. The
only
thing that has given me something different is to loop through the
key/values from params[:user]. I''m a bit stumped here, and any help is
appreciated.
Thanks,
Jeremiah
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060727/7b0d79bc/attachment.html
Jeremiah Peschka
2006-Jul-28 14:13 UTC
[Rails] Re: Creating multiple objects from form data
Curiously enough, when I run this same sequence of code through
script\console, I get the same error.
account = Account.create!(:name => "poop", :url =>
"poop")
company = account.companies.create!(:name => "poop")
user = company.users.create!(:first_name => "poop", :last_name
=> "poop",
:username => "poop", :email_address => "poop",
:password => "poop",
:password_confirmation => "poop")
produces
ActiveRecord::RecordInvalid: Validation failed: Username can''t be blank
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:736:in
`save!''
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:699:in
`create!''
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/associations/has_many_association.rb:102:in
`method_missing''
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:873:in
`with_scope''
from
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/associations/has_many_association.rb:92:in
`method_missing''
from (irb):3
Here''s the code from the user model
require ''digest/sha2''
class User < ActiveRecord::Base
belongs_to :account
belongs_to :company
belongs_to :department
belongs_to :address
belongs_to :default_project,
:class_name => ''Project'',
:foreign_key => ''default_project_id''
has_and_belongs_to_many :roles
has_and_belongs_to_many :projects
has_and_belongs_to_many :meetings
validates_uniqueness_of :username
validates_confirmation_of :password
validates_presence_of :username
#validates_presence_of :password
#validates_presence_of :password_confirmation
#validates_presence_of :first_name
#validates_presence_of :last_name
#validates_presence_of :email_address
cattr_accessor :current_user
attr_accessor :password, :password_confirmation
attr_accessible :password, :password_confirmation
def hashed_password=(pass)
salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
self.password_salt, self.password_hash = salt,
Digest::SHA256.hexdigest(pass
+ salt)
end
def self.authenticate(username, password)
user = User.find(:first, :conditions => [''username =
?'', username])
if user.blank? || Digest::SHA256.hexdigest(password + user.password_salt)
!= user.password_hash
raise "Username or password invalid"
end
user
end
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060728/38a67e97/attachment.html
Jeremiah Peschka
2006-Jul-28 14:19 UTC
[Rails] Re: Creating multiple objects from form data
Guess I didn''t read my own code close enough. I was blocking myself with attr_accessible. On 7/28/06, Jeremiah Peschka <jeremiah.peschka@gmail.com> wrote:> > Curiously enough, when I run this same sequence of code through > script\console, I get the same error. > > account = Account.create!(:name => "poop", :url => "poop") > company = account.companies.create! (:name => "poop") > user = company.users.create!(:first_name => "poop", :last_name => "poop", > :username => "poop", :email_address => "poop", :password => "poop", > :password_confirmation => "poop") > > produces > > ActiveRecord::RecordInvalid: Validation failed: Username can''t be blank > from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:736:in > `save!'' > from C:/ruby/lib/ruby/gems/1.8/gems/activerecord- 1.14.3/lib/active_record/validations.rb:699:in > `create!'' > from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/associations/has_many_association.rb:102:in > `method_missing'' > from C:/ruby/lib/ruby/gems/1.8/gems/activerecord- 1.14.3/lib/active_record/base.rb:873:in > `with_scope'' > from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/associations/has_many_association.rb:92:in > `method_missing'' > from (irb):3 > > Here''s the code from the user model > > require ''digest/sha2'' > > class User < ActiveRecord::Base > belongs_to :account > belongs_to :company > belongs_to :department > belongs_to :address > belongs_to :default_project, > :class_name => ''Project'', > :foreign_key => ''default_project_id'' > has_and_belongs_to_many :roles > has_and_belongs_to_many :projects > has_and_belongs_to_many :meetings > > validates_uniqueness_of :username > validates_confirmation_of :password > validates_presence_of :username > #validates_presence_of :password > #validates_presence_of :password_confirmation > #validates_presence_of :first_name > #validates_presence_of :last_name > #validates_presence_of :email_address > > cattr_accessor :current_user > > attr_accessor :password, :password_confirmation > attr_accessible :password, :password_confirmation > > def hashed_password=(pass) > salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp > self.password_salt, self.password_hash = salt, Digest:: > SHA256.hexdigest(pass + salt) > end > > def self.authenticate(username, password) > user = User.find(:first, :conditions => [''username = ?'', username]) > if user.blank? || Digest::SHA256.hexdigest(password + > user.password_salt) != user.password_hash > raise "Username or password invalid" > end > user > end > end >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060728/a86d10f4/attachment.html