metalic.skeleton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-24 10:46 UTC
problem: login Data save in two tables.
Hi guys, I have an user registration page. In that page there are 5 input fields. I have to save user id and password in "login" table and other 3 input in "userinfo" table. Regarding thid tropic I have two questions: 1. How can i retrive the post data from the input field? 2. How can i save data in two table by one model? I use instants rails 1.4 and mysel 5 please give me some ideas? also waitng for some sample code . Amin --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You need 2 Models obviously, with an has_one/belongs_to association. Class User < ActiveRecord::Base has_one :userinfo end Class Userinfo < ActiveRecord::Base table_name "userinfo" belongs_to end the userinfo table has to have opne column "user_id" for this association to work. then you have 5 input fields in your view: #login_form.rhml <% form_for :user, @user, :url => { :action => "login" } do |f| %> <%= f.text_field "name" %> <%= f.password_field "password" %> <% fields_for :userinfo, @user.userinfo do |u| %> <% u.text_field1 :column1 %> <% u.text_field2 :column2 %> <% u.text_field3 :column3 %> <% end %> <% end %> in your controller you can then access the data fo the 2 use fields with params[:user] (e.g. params[:user][:name]) and the userinfo with params[:userinfo] in the same way. you create an new user, and an associated userinfo item: #controller .... #Create the user (saves automatically) @user = User.create params[:user] #Create the associated Userinfo item (saves automatically) @user.create_userinfo params[:userinfo] .... If you find typos, keep them ;) On 24 Apr., 12:46, "metalic.skele...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <metalic.skele...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi guys, > > I have an user registration page. In that page there > are 5 input fields. > > I have to save user id and password in "login" table and other 3 input > in "userinfo" table. > > Regarding thid tropic I have two questions: > > 1. How can i retrive the post data from the input field? > 2. How can i save data in two table by one model? > > I use instants rails 1.4 and mysel 5 > > please give me some ideas? also waitng for some sample code . > > Amin--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---