Hello there, this is probably a very simple problem but I''m stuck. I''m following the Login generator tutorial from rubyonrails. It''s all worked fine so far. But I want to add a bit more functionality so that when a user signs up in addition to chosing a login name and password they type in a company name which is added to a seperate table called COMPANIES. The id for this record should then be added to the user record (see # in ACCOUNT_CONTROLLER.RB). Those in the know will be unsurprised to find out that this isn''t working! I''ve found lots of examples on the web for multiple table forms with select fields but not with just text fields, the difference being that with selects, the id to be copied already exists before the form is submitted, whereas in my case, the company id is created after submittal (is that a word?) of the form. Would be grateful for any help. Cheers SIGNUP.RHTML ----------- <%= start_form_tag :action=> "signup" %> <div title="Account signup" id="signupform" class="form"> <h3>Signup</h3> <%= error_messages_for ''user'' %><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 %> ACCOUNT_CONTROLLER.RB -------------------- .... def signup @user = User.new(@params[:user]) @companies = Company.new(@params[:company]) if @request.post? and @user.save @user.company_id= @companies.id # @session[:user] = User.authenticate(@user.login, @params[:user][:password]) flash[''notice''] = "Signup successful" redirect_back_or_default :action => "welcome" end end table COMPANIES --------------- CREATE TABLE companies ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(40) NOT NULL, info VARCHAR(40), modified_at DATETIME, created_at DATETIME, access DATETIME, PRIMARY KEY(id) ); table USERS ----------- CREATE TABLE users ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, login VARCHAR(40) NOT NULL, first VARCHAR(40), last VARCHAR(40), pswd VARCHAR(40) NOT NULL, modified_at DATETIME, created_at DATETIME, access DATETIME, company_id INTEGER UNSIGNED, PRIMARY KEY(id), INDEX company_users_FKIndex1 (company_id), FOREIGN KEY(company_id) REFERENCES companies(id) on DELETE CASCADE );
Perhaps you should set @user.company_id before saving the @user Hope it helps, Zsombor Adam Groves wrote:> > def signup > @user = User.new(@params[:user]) > @companies = Company.new(@params[:company]) > if @request.post? and @user.save > > > @user.company_id= @companies.id # > > > @session[:user] = User.authenticate(@user.login, > @params[:user][:password]) > flash[''notice''] = "Signup successful" > redirect_back_or_default :action => "welcome" > end > end >-- Company - http://primalgrasp.com Thoughts - http://deezsombor.blogspot.com
Hello there, this is probably a very simple problem but I'm stuck. I'm following the Login generator tutorial from rubyonrails. It's all worked fine so far. But I want to add a bit more functionality so that when a user signs up in addition to chosing a login name and password they type in a company name which is added to a seperate table called COMPANIES. The id for this record should then be added to the user record (see # in ACCOUNT_CONTROLLER.RB). Those in the know will be unsurprised to find out that this isn't working! I've found lots of examples on the web for multiple table forms with select fields but not with just text fields, the difference being that with selects, the id to be copied already exists before the form is submitted, whereas in my case, the company id is created after submittal (is that a word?) of the form. Would be grateful for any help. Cheers SIGNUP.RHTML ----------- <%= start_form_tag :action=> "signup" %> <div title="Account signup" id="signupform" class="form"> <h3>Signup</h3> <%= error_messages_for 'user' %><br/> <label for="company_name">Company:</l�abel><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_confirmatio�n">Confirm password:</label><br/> <%= password_field "user", "password_confirmation", :size => 30 %><br/> <input type="submit" value="Signup »" class="primary" /> <%= end_form_tag %> ACCOUNT_CONTROLLER.RB -------------------- .... def signup @user = User.new(@params[:user]) @companies = Company.new(@params[:company]) if @request.post? and @user.save @user.company_id= @companies.id # @session[:user] = User.authenticate(@user.login, @params[:user][:password]) flash['notice'] = "Signup successful" redirect_back_or_default :action => "welcome" end end table COMPANIES --------------- CREATE TABLE companies ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(40) NOT NULL, info VARCHAR(40), modified_at DATETIME, created_at DATETIME, access DATETIME, PRIMARY KEY(id) ); table USERS ----------- CREATE TABLE users ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, login VARCHAR(40) NOT NULL, first VARCHAR(40), last VARCHAR(40), pswd VARCHAR(40) NOT NULL, modified_at DATETIME, created_at DATETIME, access DATETIME, company_id INTEGER UNSIGNED, PRIMARY KEY(id), INDEX company_users_FKIndex1 (company_id), FOREIGN KEY(company_id) REFERENCES companies(id) on DELETE CASCADE ); -- To find out what we're up to visit http://prenzlberg.blogspot.com _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I have been experiencing somewhat similar problems, I think. What error message have you been seeing? Mine has shown MySQL complaining about "packets out of order". P.S. I think the word you''re looking for is "submission". On Fri, 02 Sep 2005 04:56:38 -0500, Adam Groves <adam.groves-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello there, > > > this is probably a very simple problem but I''m stuck. > > > I''m following the Login generator tutorial from rubyonrails. It''s all > worked fine so far. But I want to add a bit more functionality so that > when a user signs up in addition to chosing a login name and password > they type in a company name which is added to a seperate table called > COMPANIES. The id for this record should then be added to the user > record (see # in ACCOUNT_CONTROLLER.RB). > > > Those in the know will be unsurprised to find out that this isn''t > working! I''ve found lots of examples on the web for multiple table > forms with select fields but not with just text fields, the difference > being that with selects, the id to be copied already exists before the > form is submitted, whereas in my case, the company id is created after > submittal (is that a word?) of the form. > > > Would be grateful for any help. > > > Cheers > > > SIGNUP.RHTML > ----------- > > > <%= start_form_tag :action=> "signup" %> > > > <div title="Account signup" id="signupform" class="form"> > <h3>Signup</h3> > <%= error_messages_for ''user'' %><br/> > <label for="company_name">Company:</l�abel><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_confirmatio�n">Confirm > password:</label><br/> > <%= password_field "user", "password_confirmation", :size => 30 > %><br/> > > > <input type="submit" value="Signup »" class="primary" /> > > > <%= end_form_tag %> > > > ACCOUNT_CONTROLLER.RB > -------------------- > > > .... > > > def signup > @user = User.new(@params[:user]) > @companies = Company.new(@params[:company]) > if @request.post? and @user.save > > > @user.company_id= @companies.id # > > > @session[:user] = User.authenticate(@user.login, > @params[:user][:password]) > flash[''notice''] = "Signup successful" > redirect_back_or_default :action => "welcome" > end > end > > > table COMPANIES > --------------- > > > CREATE TABLE companies ( > id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, > name VARCHAR(40) NOT NULL, > info VARCHAR(40), > modified_at DATETIME, > created_at DATETIME, > access DATETIME, > PRIMARY KEY(id) > ); > > > table USERS > ----------- > > > CREATE TABLE users ( > id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, > login VARCHAR(40) NOT NULL, > first VARCHAR(40), > last VARCHAR(40), > pswd VARCHAR(40) NOT NULL, > modified_at DATETIME, > created_at DATETIME, > access DATETIME, > company_id INTEGER UNSIGNED, > PRIMARY KEY(id), > INDEX company_users_FKIndex1 (company_id), > FOREIGN KEY(company_id) REFERENCES companies(id) on DELETE CASCADE > ); > >
This sounds like a classic case of overnormalization to me. With the information you've provided, I'm not seeing the point in maintaining a seperate lookup table for companies, as you have no guarantee that company names won't be duplicated, as every new user would create a new entry into the companies table (if you aren't doing any kind of duplicate checking). Personally, I would just use a varchar field in the users table to represent the company name, drop the companies table and then you don't have to worry about it. Now, if you have a good reason to maintain a companies table (from the info you have provided, i'm not seeing one), then you would first have to check if the company name the user entered already existed. if it does, extract the id and set the user.company_id. if it doesn't, create an entry in the companies table, get the id, set the user.company_id and finally save the user. the problem with this situation is that what if userA comes along and signs up with Acme, Inc. you would create a new entry in the companies table to represent this new company...then userB comes along, who works for the same company, but he enters Acme, Incorporated. ah...NEW company again....and then userC comes along and enters Acme INC....and so on...see whats happening here? On 9/2/05, Christopher Singley <csingley@singleycapital.com> wrote:> I have been experiencing somewhat similar problems, I think. > What error message have you been seeing? > Mine has shown MySQL complaining about "packets out of order". > > P.S. I think the word you're looking for is "submission". > > On Fri, 02 Sep 2005 04:56:38 -0500, Adam Groves <adam.groves@gmail.com> > wrote: > > > Hello there, > > > > > > this is probably a very simple problem but I'm stuck. > > > > > > I'm following the Login generator tutorial from rubyonrails. It's all > > worked fine so far. But I want to add a bit more functionality so that > > when a user signs up in addition to chosing a login name and password > > they type in a company name which is added to a seperate table called > > COMPANIES. The id for this record should then be added to the user > > record (see # in ACCOUNT_CONTROLLER.RB). > > > > > > Those in the know will be unsurprised to find out that this isn't > > working! I've found lots of examples on the web for multiple table > > forms with select fields but not with just text fields, the difference > > being that with selects, the id to be copied already exists before the > > form is submitted, whereas in my case, the company id is created after > > submittal (is that a word?) of the form. > > > > > > Would be grateful for any help. > > > > > > Cheers > > > > > > SIGNUP.RHTML > > ----------- > > > > > > <%= start_form_tag :action=> "signup" %> > > > > > > <div title="Account signup" id="signupform" class="form"> > > <h3>Signup</h3> > > <%= error_messages_for 'user' %><br/> > > <label for="company_name">Company:</l�abel><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_confirmatio�n">Confirm > > password:</label><br/> > > <%= password_field "user", "password_confirmation", :size => 30 > > %><br/> > > > > > > <input type="submit" value="Signup »" class="primary" /> > > > > > > <%= end_form_tag %> > > > > > > ACCOUNT_CONTROLLER.RB > > -------------------- > > > > > > .... > > > > > > def signup > > @user = User.new(@params[:user]) > > @companies = Company.new(@params[:company]) > > if @request.post? and @user.save > > > > > > @user.company_id= @companies.id # > > > > > > @session[:user] = User.authenticate(@user.login, > > @params[:user][:password]) > > flash['notice'] = "Signup successful" > > redirect_back_or_default :action => "welcome" > > end > > end > > > > > > table COMPANIES > > --------------- > > > > > > CREATE TABLE companies ( > > id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, > > name VARCHAR(40) NOT NULL, > > info VARCHAR(40), > > modified_at DATETIME, > > created_at DATETIME, > > access DATETIME, > > PRIMARY KEY(id) > > ); > > > > > > table USERS > > ----------- > > > > > > CREATE TABLE users ( > > id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, > > login VARCHAR(40) NOT NULL, > > first VARCHAR(40), > > last VARCHAR(40), > > pswd VARCHAR(40) NOT NULL, > > modified_at DATETIME, > > created_at DATETIME, > > access DATETIME, > > company_id INTEGER UNSIGNED, > > PRIMARY KEY(id), > > INDEX company_users_FKIndex1 (company_id), > > FOREIGN KEY(company_id) REFERENCES companies(id) on DELETE CASCADE > > ); > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Christopher and Chris (and anyone else), Thanks for the replies. Christopher: I''ve not been getting any error messages. Hmmph Chris: Here''s my basic concept: 1. User A signs up 2. User A assigned role allowing him/her to create other accounts. User A creats User B (and User B is assigned company_id of User A) 3. User B logs on So you see, many user''s will belong to the same company. I''ll need to validate the company name at sign up to prevent duplication, but I haven''t got that far yet. BTW: just ordered Agile Web Developement !!
Adam Groves wrote:> Hello there,Hi, maybe I am a bit naive here but: your User belong_to a Company so:> .... > > > def signup > @user = User.new(@params[:user]) > @companies = Company.new(@params[:company])wouldn''t just: @user.company= @companies here do the trick?> if @request.post? and @user.save >when you save the user object it should add the companies id to it''s table (relation columns shouldn''t be directly used when appropriately marking the relations into the model). Btw I''m not sure if you have to first save the company bye, Luca
then why not offer this on the user add/edit views...a select field of existing companies in addition to a text field to allow entry of a new company if they don''t see what they want in the select... On 9/2/05, Adam Groves <adam.groves-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Christopher and Chris (and anyone else), > > Thanks for the replies. > > Christopher: > I''ve not been getting any error messages. Hmmph > > Chris: > Here''s my basic concept: > > 1. User A signs up > 2. User A assigned role allowing him/her to create other accounts. > User A creats User B (and User B is assigned company_id of User A) > 3. User B logs on > > So you see, many user''s will belong to the same company. I''ll need to > validate the company name at sign up to prevent duplication, but I > haven''t got that far yet. > > BTW: just ordered Agile Web Developement !! > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >