Nnah,

As a start, you could look again at the last tutorial "RailsAssociations";
there the view posts/show.html.erb presents a form to enter a 'comment'
(that is like a nested resource/model, as 'comments' are within a 'post');
this is not different from what you are asking.

Now, in your case the resources are Person and Student; by the way, allow me
to use the names of 'Teacher' and Student (as Person/Student sounds more
like an inheritance than a containment relationship, and if so, we would be
talking about something different).
So you could do the same thing that the tutorial does; but instead of
presenting the form for the student in the Teacher page, you could add a
link in view teachers/show to "Add a Student" (so to preserve the student
views in their own files):

 <%= link_to "New Student", :controller => "student", :action => "new" %>

or (in the REST way):

 <%= link_to "New Student", new_student_path %>

However, in the Student controller you would need the 'teacher_id' (to be
able to retrieve the teacher, and add the student to his student
collection); the class exercise used the flash (to carry the 'post_id'), and
you could do the same, just to experiment.

But here we would depart from the RESTful Rails approach to nested
resources. This is a long story, and we are jumping over several exercises;
but just to give you a flavor, it boils down to have in the URL both the
teacher_id and the student_id (if the second exists); for example:

  GET  /teachers/3/students/new   # get the form 'new' to create a student
  POST /teachers/3/students         # create a student for teacher 3
  GET  /teachers/3/students/37      # show student 37 for teacher 3

This allows the student actions to retrieve with params[:teacher_id] the id
of the teacher.

Now, to obtain those Urls (and the correspondent helpers to generate them)
we would need to configure routes with something like (in routes.rb):
  map.resources :teachers, :has_many => :students

We have gone a bit too far and it would not be productive to begin to play
with this now. My advice would be to proceed with the classes in order:
learn well Controllers, Views and Routes, before attacking the problem you
posed.

One last observation: when we have nested resources, it is usually best to
avoid scaffolding the nested ones (in your case, the student), for 2
reasons:
a) currently, scaffolding is not aware that the resource is 'nested', and
does not generate the correct urls; so, one would have to change them
manually (painful exercise).
b) quite a bit of the code for the nested resource usually needs to be
changed or removed, as the resource is managed from within the outer
resource.

So, to come back to your case, the best should be to scaffold the Teacher,
and manually write the code for the Student.

###
I know that the above is far from simple and clear, but you have asked a
question that simply involves all the rest of the course, and I wanted to
give you some elements that you could begin to research.

In any case, I believe things will become clear if you proceed methodically
with the course.

Raul


On Thu, Oct 16, 2008 at 8:44 AM, Nnah <[EMAIL PROTECTED]> wrote:

>
> Hello,
> Maybe someone can help me. I am trying to get my head around Lesson 10
> Active Record Association basic. I am comfortable with the information
> that goes into the model (has_many or belong_to) I am very comfortable
> with that. But I get lost within the controller and the Views. I can
> replicate this example but when I try a different example then I get
> confused. So maybe someone can explain it with a different example.
>
> So imagine I have 2 models, a person and a student model. The student
> model belongs to the person model.
> So lets assume the person model has (lastname, firstname and sex)
> while the student model has(studentID, address, yr)
>
> class Person < ActiveRecord::Base
>  validates_presence_of :lastname, :firstname, :sex
>  has_many :students
> end
> class Student < ActiveRecord::Base
>  validates_presence_of :studentID, :address, :yr
>  belongs_to :person
> end
>
> This is where I get confused. I have used scaffolding for both the
> person and the student. So what if I want to be able to create a
> person, then during that process have an option to create a student
> that belongs to that particular person model. How can I modify the
> controller and also the views that's were I am stuck.
>
> If my example is not concrete enough someone can suggest a different
> example to explain the concept.
>
> I really appreciate it.
> Thanks
> Ike
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to