I have Person model in that i have used STI like class person < ActiveRecord class student <person class parent < person for every thing i have table called people. when i am adding a student i have fields for parent also like i want to add a student with firstname and last name and at the same time i mean in the same form i wnat to add to which parent he belongs to in the same form i have fields like parent name, email, address can u give idea abt this --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
cool wrote:> I have Person model in that i have used STI like > class person < ActiveRecord > class student <person > class parent < person > > for every thing i have table called people. > > when i am adding a student i have fields for parent also like > i want to add a student with firstname and last name and at the same > time i mean in the same form i wnat to add to which parent he belongs > to in the same form i have fields like parent name, email, addressThis was quite hard to read. Use full stops or bullet points or something.> > can u give idea abt thisSo, you have something like class Person < ActiveRecord::Base ... in app/models/person.rb And: class Parent < Person ... class Student < Person ... maybe in app/models/parent.rb and student.rb respectively. And your ''people'' table has a column called ''type'' which is a string field. In your edit form you could do something like: <% form_for :student , :url => {:action => ''update''} do |s| %> <%= s.text_field :first_name %> <%= s.text_field :last_name %> ... parent stuff - see below ... ... submit button etc ... <% end %> Within this form you would have something like: <%= select :selected_parent , :id , Parent.find(:all).collect{|p|[p.last_name,p.id]} %> which will generate a dropdown with existing parents last_names and their id''s. Or if you are entering parents details in alongside the student, something like: <%= text_field :parent, :first_name %> <%= text_field :parent, :last_name %> Read api.rubyonrails.com and go to ActionView::Helpers::FormHelper and FormOptions for how to use the form fields and what they generate in terms of the params your controller will be receiving. In your controller, the ''update'' action would do something like: def update ... @student=Student.new(params[:student]) @student.parent_id = params[:selected_parent][:id] @student.save! (''parent_id'' is a field for storing the id of the parent in your ''people'' table.) You could save the parent using associations instead but I won''t go into it. If you were collecting parent details using the text fields above, you might have: @parent=Parent.new(params[:parent]) @parent.save! Daniel -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
this is my model class Person < ActiveRecord::Base end class parent < Person end class Student < person end class principal<person end i have table called people. iam adding all the details of parent and student and principal in the same table. my form is <% form_for :student do %> <td><%= text_field :first_name, :label => "Student" %> <td><%= text_field :last_name %></td> <% fields_for :parent %> <tr> <td><%= text_field :first_name, :label => "Parents/ Guardian" %></td> <td><%= text_field :last_name %></td> </tr> <tr> <td><%= text_field :email, :label => "Email" %></td> </tr> <tr> <td><%= text_field :phone, :label => "Phone" %></td> </tr> <% end %> <% end %> In the New action i have provided like def new @student = Student.new @parent = Parent.new end my fields in people table are t.column :type, :string t.column :first_name, :string t.column :last_name, :string t.column :email, :string t.column :phone, :string t.column :address, :text how can we add the student details(first_name, last_name) and the parent details(first_name, last_name, email, phone) at the same time with in the same table called people. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Cool, This post didn''t make it onto ruby-forum.com but it is on google. On Oct 2, 11:10 pm, cool <eshward...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> this is my model > class Person < ActiveRecord::Base > end > class parent < Person > end > class Student < person > end > class principal<person > endObviously the above ''person'' and ''principal'' are capitalized.> i have table called people. > iam adding all the details of parent and student and principal in the > same table.yes> > my form is > <% form_for :student do %>Not much point doing this if you don''t include the parameter. Also, where is your form posting to? <% form_for :student , :url => {:action => ''update''} do |s| %>> <td><%= text_field :first_name, :label => "Student" %> > <td><%= text_field :last_name %></td>Then you can say: <td><%= s.text_field :first_name, :label => "Student" %> Or <td><%= text_field :student , :first_name, :label => "Student" %> which is the same thing.> <% fields_for :parent %>You don''t need "fields_for" - I don''t think you''re using it below.> <tr> > <td><%= text_field :first_name, :label => "Parents/ > Guardian" %></td>So instead, it would be something like: <td><%= text_field :parent , :first_name, :label => "Parents/ Guardian" %></td> And similarly for the rest of these:> <td><%= text_field :last_name %></td> > </tr> > <tr> > <td><%= text_field :email, :label => "Email" %></td> > </tr> > <tr> > <td><%= text_field :phone, :label => "Phone" %></td> > </tr> > <% end %> > <% end %> >See my previous post if you want a dropdown selection box; ie <%select ... %>.> In the New action i have provided like > > def new > @student = Student.new > @parent = Parent.new > > endok; you could probably even omit creating these.> my fields in people table are > t.column :type, :string > t.column :first_name, :string > t.column :last_name, :string > t.column :email, :string > t.column :phone, :string > t.column :address, :textok> > how can we add the student details(first_name, last_name) and the > parent details(first_name, last_name, email, phone) at the same time > with in the same table called people.So: <td><%= text_field :parent , :first_name, :label => "Parents/ Guardian" %></td> will create something like <input type="text" name="parent[first_name]" ... /> The important thing is the name attribute. It will be converted to params[:parent][:first_name] etc Similarly for principal: <td><%= text_field :principal , :first_name %></td> So in your update action of your controller, you then say: @parent=Parent.new(params[:parent]) and similarly for @student and @principal. Regards, Daniel --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 3, 8:22 am, Daniel <dlb.id...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Cool, > This post didn''t make it onto ruby-forum.com but it is on google. > > On Oct 2, 11:10 pm, cool <eshward...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > this is my model > > class Person < ActiveRecord::Base > > end > > class parent < Person > > end > > class Student < person > > end > > class principal<person > > end > > Obviously the above ''person'' and ''principal'' are capitalized. > > > i have table called people. > > iam adding all the details of parent and student and principal in the > > same table. > > yes > > > > > my form is > > <% form_for :student do %> > > Not much point doing this if you don''t include the parameter. Also, > where is your form posting to? > <% form_for :student , :url => {:action => ''update''} do |s| %> >Sorry. form_for is handy; whether you''re passing the parameter in or not. It will generate an authenticity token as well as just being more convenient. Read up on api.rubyonrails.com. See also form_tag.> > <td><%= text_field :first_name, :label => "Student" %> > > <td><%= text_field :last_name %></td> > > Then you can say: > <td><%= s.text_field :first_name, :label => "Student" %> > Or > <td><%= text_field :student , :first_name, :label => "Student" > %> > which is the same thing. > > > <% fields_for :parent %> > > You don''t need "fields_for" - I don''t think you''re using it below. > > > <tr> > > <td><%= text_field :first_name, :label => "Parents/ > > Guardian" %></td> > > So instead, it would be something like: > <td><%= text_field :parent , :first_name, :label => "Parents/ > Guardian" %></td> > > And similarly for the rest of these: > > > <td><%= text_field :last_name %></td> > > </tr> > > <tr> > > <td><%= text_field :email, :label => "Email" %></td> > > </tr> > > <tr> > > <td><%= text_field :phone, :label => "Phone" %></td> > > </tr> > > <% end %> > > <% end %> > > See my previous post if you want a dropdown selection box; ie <%> select ... %>. > > > In the New action i have provided like > > > def new > > @student = Student.new > > @parent = Parent.new > > > end > > ok; you could probably even omit creating these. > > > my fields in people table are > > t.column :type, :string > > t.column :first_name, :string > > t.column :last_name, :string > > t.column :email, :string > > t.column :phone, :string > > t.column :address, :text > > ok > > > > > how can we add the student details(first_name, last_name) and the > > parent details(first_name, last_name, email, phone) at the same time > > with in the same table called people. > > So: > <td><%= text_field :parent , :first_name, :label => "Parents/ > Guardian" %></td> > will create something like > <input type="text" name="parent[first_name]" ... /> > The important thing is the name attribute. > It will be converted to > params[:parent][:first_name] > etc > > Similarly for principal: > <td><%= text_field :principal , :first_name %></td> > > So in your update action of your controller, you then say: > @parent=Parent.new(params[:parent]) > and similarly for @student and @principal. > > Regards, > Daniel--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
u didnt tell me how can we add in the create action at the same time for people table can u send me the code how can we add or an idea of that? On Oct 3, 3:47 am, Daniel <dlb.id...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 3, 8:22 am, Daniel <dlb.id...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi Cool, > > This post didn''t make it onto ruby-forum.com but it is on google. > > > On Oct 2, 11:10 pm, cool <eshward...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > this is my model > > > class Person < ActiveRecord::Base > > > end > > > class parent < Person > > > end > > > class Student < person > > > end > > > class principal<person > > > end > > > Obviously the above ''person'' and ''principal'' are capitalized. > > > > i have table called people. > > > iam adding all the details of parent and student and principal in the > > > same table. > > > yes > > > > my form is > > > <% form_for :student do %> > > > Not much point doing this if you don''t include the parameter. Also, > > where is your form posting to? > > <% form_for :student , :url => {:action => ''update''} do |s| %> > > Sorry. form_for is handy; whether you''re passing the parameter in or > not. > It will generate an authenticity token as well as just being more > convenient. > Read up on api.rubyonrails.com. See also form_tag. > > > > <td><%= text_field :first_name, :label => "Student" %> > > > <td><%= text_field :last_name %></td> > > > Then you can say: > > <td><%= s.text_field :first_name, :label => "Student" %> > > Or > > <td><%= text_field :student , :first_name, :label => "Student" > > %> > > which is the same thing. > > > > <% fields_for :parent %> > > > You don''t need "fields_for" - I don''t think you''re using it below. > > > > <tr> > > > <td><%= text_field :first_name, :label => "Parents/ > > > Guardian" %></td> > > > So instead, it would be something like: > > <td><%= text_field :parent , :first_name, :label => "Parents/ > > Guardian" %></td> > > > And similarly for the rest of these: > > > > <td><%= text_field :last_name %></td> > > > </tr> > > > <tr> > > > <td><%= text_field :email, :label => "Email" %></td> > > > </tr> > > > <tr> > > > <td><%= text_field :phone, :label => "Phone" %></td> > > > </tr> > > > <% end %> > > > <% end %> > > > See my previous post if you want a dropdown selection box; ie <%> > select ... %>. > > > > In the New action i have provided like > > > > def new > > > @student = Student.new > > > @parent = Parent.new > > > > end > > > ok; you could probably even omit creating these. > > > > my fields in people table are > > > t.column :type, :string > > > t.column :first_name, :string > > > t.column :last_name, :string > > > t.column :email, :string > > > t.column :phone, :string > > > t.column :address, :text > > > ok > > > > how can we add the student details(first_name, last_name) and the > > > parent details(first_name, last_name, email, phone) at the same time > > > with in the same table called people. > > > So: > > <td><%= text_field :parent , :first_name, :label => "Parents/ > > Guardian" %></td> > > will create something like > > <input type="text" name="parent[first_name]" ... /> > > The important thing is the name attribute. > > It will be converted to > > params[:parent][:first_name] > > etc > > > Similarly for principal: > > <td><%= text_field :principal , :first_name %></td> > > > So in your update action of your controller, you then say: > > @parent=Parent.new(params[:parent]) > > and similarly for @student and @principal. > > > Regards, > > Daniel--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 3, 3:15 pm, cool <eshward...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> u didnt tell me how can we add in the create action at the same time > for people table > can u send me the code how can we add or an idea of that? >For the controller and the html form, nothing is stopping you from editing and creating multiple Person objects (Student, Parent, or Principal) within the one action. Get familiar with rails form helpers as mentioned previously. Here''s a tip: think in terms of the ''name'' attribute in your form: If it''s <input name="student[first_name]" value="fred" type="text" ... /> then you''ll get params[:student][:first_name] => ''fred'' in your controller (when you submit). Similarly for <input name="parent[first_name]" ... /> you''ll get params[:parent][:first_name] I''ve also mentioned how to generate a dropdown instead of a text field if you want to assign an existing parent to a student. Then when this form is submitted to your controller you would have something like: def update @student=Student.new(params[:student]) # If you are assigning parent to student. @student.parent_id = params[:parent][:id] ## if you have dropdown @student.save! ... ## If you are creating the parent alongside the student.. @parent=Parent.new(params[:parent]) @parent.save! ... end Find some tutorials, read the main pages at api.rubyonrails.com and buy the rails book http://www.pragprog.com/titles/rails2/agile-web-development-with-rails . If you''re having trouble with rails basics, maybe skip STI for a bit and just use separate classes. Daniel --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---