Hi there
I’ve got a user model and an address model; a user has_many addresses.
A user object can be created via an html form which also allows for an
initial address to be created.
I’ve got a controller method working to save both objects via the
form, but I suspect I can achieve this in a more rails-esque manner.
Here is my controller method:
def signup
@user = User.new(params[:user])
@address = Address.new(params[:address])
return unless request.post?
if @user.save
@address.user_id = @user.id
if @address.save
flash[:notice_good] = “Your account needs to be now be
activated. You have been sent a confirmation email with activation
instructions.”
redirect_to :controller => “/shop”, :action => “index”
end
end
end
Maybe this is OK, but any clues would be appreciated.
Thanks Mark - that all makes sense.
It leads to another question however:
How to display validation errors for the address object along with
those for the user object? At present I can only get “address can not
be blank” to display, whereas I would like to see a field-by-field
listing of errors as I get with user.
Richard S. wrote:
@user = User.new(params[:user])
end
end
Maybe this is OK, but any clues would be appreciated.
What you’ve done is fine, but perhaps somewhat railier would be:
@user = User.new(params[:user])
@address = @user.addresses.build(params[:address])
if request.post? && @user.save
flash[:notice_good] = …
redirect_to :controller => :shop
end
–
We develop, watch us RoR, in numbers too big to ignore.
Richard S. wrote:
How to display validation errors for the address object along with
those for the user object? At present I can only get “address can not
be blank” to display, whereas I would like to see a field-by-field
listing of errors as I get with user.
Do you have “error_messages_for :address” in your view?
By the way, I was wrong when I said your original code was fine, because
the user is saved even if the address doesn’t validate. With the code
I gave Rails does automatic validation of the whole associated pair
before any save is done.
–
We develop, watch us RoR, in numbers too big to ignore.
Do you have “error_messages_for :address” in your view?
Yes, after using your code I can now display error_messages_for
:address. Thank you
I’d like to consolidate them however with the messages for :user and
suspect that involves hacking my own helper?
I’m also trying to functionally test the signup controller. In my
functional test I have a create_user method which gets called by my
functional tests:
protected
def create_user(options = {})
post :signup, :user =>
{
:login => ‘quire’,
:email => ‘[email protected]’,
:password => ‘quire’,
:password_confirmation => ‘quire’,
:first_name => ‘quire’,
:last_name => ‘quire’
}.merge(options)
end
But since requiring that address fields are validated before a user
can be saved, I need to ensure that dummy address attributes are added
to my create_user method above so that I can test the account
controller methods properly.
This is where my lack of Ruby shows through…any clues appreciated.