I have 2 tables (and associated controllers) Users and Companies. User
belongs to Company. User accounts are generated by the Login Generator, so I
also have an Account controller.
I''ve modified the signup method of the Account controller:
def signup
@company = Company.new(@params[:company])
@user = User.new(@params[:user])
@user.company = @company
@user.admin = false
if @request.post? and @user.save and @company.save
@session[:user] = User.authenticate(@user.login, @params[:user][:password])
flash[''notice''] = "Signup successful"
redirect_back_or_default :action => "welcome"
end
end
My User model does the following validation:
validates_uniqueness_of :login, :on => :create
validates_confirmation_of :password
validates_length_of :login, :within => 3..40
validates_length_of :password, :within => 5..40
validates_presence_of :login, :password, :password_confirmation
and my Company model:
validates_presence_of :name
my signup.rhtml forms looks like this:
<%= start_form_tag :action=> "signup" %>
<div title="Account signup" id="signupform"
class="form">
<h3>Signup</h3>
<%= error_messages_for ''user'' %><br/>
<%= error_messages_for ''company'' %><br/>
<label for="user_name">Name:</label><br/>
<%= text_field "user", "name", :size => 30
%><br/>
<label for="company_name">Company:</label><br/>
<%= text_field "company", "name", :size => 30
%><br/>
<label for="user_login">Desired login:</label><br/>
<%= text_field "user", "login", :size => 30
%><br/>
<label for="user_password">Choose
password:</label><br/>
<%= password_field "user", "password", :size => 30
%><br/>
<label for="user_password_confirmation">Confirm
password:</label><br/>
<%= password_field "user", "password_confirmation", :size
=> 30 %><br/>
<input type="submit" value="Signup »"
class="primary" />
<%= end_form_tag %>
This is really long-winded but I''m nearly done;)
If I submit the form empty, only the errors for the user are displayed. If I
submit the form leaving the company name empty, the errors are shown for the
company (''name cannot be empty'') but the user is saved anyway
with an empty
company_id field. What am I doing wrong? Is it possible to validate two
models in one form?
Cheers
Adam
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Mark Reginald James
2005-Oct-20 11:50 UTC
Re: Validating more than one model in a signup form
Adam Groves wrote:> if @request.post? and @user.save and @company.save > ... > If I submit the form empty, only the errors for the user are displayed. > If I submit the form leaving the company name empty, the errors are > shown for the company (''name cannot be empty'') but the user is saved > anyway with an empty company_id field. What am I doing wrong? Is it > possible to validate two models in one form?With the code you''ve used, if @user.save returns false, @company.save won''t even be executed. One option is to do @user.save only, validating the company by having a "validates_associated :company" line in your user model. Or you could write: if @request.post? valid_user = @user.valid? valid_company = @company.valid? if valid_user and valid_company @user.save # will also save @company because you set @user.company = @company ... -- We develop, watch us RoR, in numbers too big to ignore.
def signup
...
@user.company = @company
if @user.valid? and @company.valid?
@user.save
@company.save
end
...
end
On 10/20/05, Adam Groves
<adam.groves-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
>
> I have 2 tables (and associated controllers) Users and Companies. User
> belongs to Company. User accounts are generated by the Login Generator, so
I
> also have an Account controller.
>
> I''ve modified the signup method of the Account controller:
>
> def signup
> @company = Company.new(@params[:company])
> @user = User.new(@params[:user])
> @user.company = @company
> @user.admin = false
>
> if @request.post? and @user.save and @company.save
> @session[:user] = User.authenticate(@user.login,
> @params[:user][:password])
> flash[''notice''] = "Signup successful"
> redirect_back_or_default :action => "welcome"
> end
> end
>
> My User model does the following validation:
>
> validates_uniqueness_of :login, :on => :create
>
> validates_confirmation_of :password
> validates_length_of :login, :within => 3..40
> validates_length_of :password, :within => 5..40
> validates_presence_of :login, :password, :password_confirmation
>
> and my Company model:
>
> validates_presence_of :name
>
> my signup.rhtml forms looks like this:
>
> <%= start_form_tag :action=> "signup" %>
>
>
> <div title="Account signup" id="signupform"
class="form">
> <h3>Signup</h3>
> <%= error_messages_for ''user'' %><br/>
> <%= error_messages_for ''company'' %><br/>
>
> <label for="user_name">Name:</label><br/>
> <%= text_field "user", "name", :size => 30
%><br/>
> <label
for="company_name">Company:</label><br/>
> <%= text_field "company", "name", :size => 30
%><br/>
> <label for="user_login">Desired
login:</label><br/>
> <%= text_field "user", "login", :size => 30
%><br/>
> <label for="user_password">Choose
password:</label><br/>
> <%= password_field "user", "password", :size =>
30 %><br/>
> <label for="user_password_confirmation">Confirm
> password:</label><br/>
> <%= password_field "user",
"password_confirmation", :size => 30 %><br/>
>
> <input type="submit" value="Signup »"
class="primary" />
>
> <%= end_form_tag %>
>
> This is really long-winded but I''m nearly done;)
>
> If I submit the form empty, only the errors for the user are displayed. If
I
> submit the form leaving the company name empty, the errors are shown for
the
> company (''name cannot be empty'') but the user is saved
anyway with an empty
> company_id field. What am I doing wrong? Is it possible to validate two
> models in one form?
>
> Cheers
>
> Adam
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
Mark Reginald James
2005-Oct-20 12:11 UTC
Re: Validating more than one model in a signup form
Mark Reginald James wrote:> @user.save # will also save @company because you set @user.company = @company@user.save(false) would be better because this prevents an unnecessary second validation. -- We develop, watch us RoR, in numbers too big to ignore.