On Jul 19, 11:33 pm, Jen <jen.bot...@gmail.com> wrote:
> I forgot to mention that currently each of my tables is handled by a
> separate controller.

A controller is simply an entry/access point, you can pull in data
from any model in any controller and pass it to any view to be used.

>
> I thought this would be ok and anything entered in my resources _form
> partial would be handled by the 'resources' controller etc. However

No.

The form 'decides' which controller to pass _all_ the form data to,
you then need to ensure that the receiving controller saves/updates
the model and its relations.

> looking at the rails cast source code has confused me a bit, as it seems
> that all the tables are managed by one controller. Are my nested fields

Controllers don't 'manage' tables, they simply collect model data and
pass it to a view, any required business logic should be handled by
the models. You should keep the code in your controllers to a minimum,
if you find that you are beginning to bloat your controller actions,
or start adding loads of private methods, then you need to look at
your code, and find a way to re-factor out into helpers or models or
lib modules etc.

For Example;

class ExamplesController < ApplicationController
  def show
    @example = Example.find(params[:id])
    @authors = Author.all
    render 'show'
  end
end

views/examples/show.html.erb
# This view can now display the example by accessing @example
# It can also display a list of authors of all the examples by
accessing @authors
# So you can create whatever data you want in your controller, and it
can be accessed in the view that is rendered.


> not rendering because the universities table and resources have their
> own totally separate controllers?

No.

You need to build any relations prior to rendering the view, you do
this in your new and edit actions in your controller.

Example:

class Person < ActiveRecord::Base
  belongs_to :address
  accepts_nested_attributes_for :address
end

class Address < ActiveRecord::Base
 has_many :people
 accepts_nested_attributes_for :people
end

class PeopleController < ApplicationController
  def new
    @person = Person.new
    @person.build_address
  end

  def edit
    @person = Person.find(params[:id])
    @person.build_address unless @person.address #don't build an
address if the person already has one
  end
end

class AddressController < ApplicationController
  def new
    @address = Address.new
    @address.people.build
  end

  # This is untested, I haven't needed to set up a nested form for a
model that has a has_many relation
  def edit
    @address = Address.find(params[:id])
    @address.people.build unless @address.people.size > 0 #don't build
people if the address already has people
  end
end

Also, if you take a look at your Resources form, unless you have
removed some of the code, it is not a form, just a list of fields for
a form. If you try and use that partial on it's own, you will not be
able to.

When I want to re-use form fields in other forms, I move the fields
out into a separate partial and render it as you have done in your
University form. The only difference, is that I create an assets
folder in the relevant view folder for any additional 'out of the
norm' views/partials. Surprisingly, I call the partial
_form_fields..... Then, both the University form, and the resources
form can both use the same views/resources/assets/
_form_fields.html.erb partial.

The fields can then be used by any other nested form. So, if you
decide to add another model that utilises the resources model, you can
easily create another nested form by calling in the resources
_form_fields partial.

HTH

Paul
>
> I will be experimenting with this more tomorrow. If someone could
> clarify this point about the controllers it would really help.
>
> Thanks,
> Jen!
>
> On 19/07/11 16:49, Jen wrote:
>
>
>
> >     Hi Barney,
>
> > I believe I am having the same problem you experienced, though my code
> > is a little different. If you or anyone else can tell me how to solve
> > it that would be great!
>
> > I am creating a db of Universities and resources. One University can
> > have many resources, but one resource can only come from one
> > University. I am trying to create a form with nested attributes, so
> > hat users wil be encouraged to give details about the University where
> > the resource comes from.
>
> > Code from views/universities/_form:
>
> > <%= form_for(@university) do |f| %>
> > <% if @university.errors.any? %>
> > <div id="error_explanation">
> > <h2><%= pluralize(@university.errors.count, "error") %> prohibited
> > this university from being saved:</h2>
>
> > <ul>
> > <% @university.errors.full_messages.each do |msg| %>
> > <li><%= msg %></li>
> > <% end %>
> > </ul>
> > </div>
> > <% end %>
>
> > <div class="field">
> > <%= f.label :name %><br />
> > <%= f.text_field :name %>
> > </div>
> > <div class="field">
> > <%= f.label :address %><br />
> > <%= f.text_field :address %>
> > </div>
> > <div class="field">
> > <%= f.label :country %><br />
> > <%= f.text_field :country %>
> > </div>
>
> > <% f.fields_for :resources do |builder| %>
> > <%= render "resources/form", :f => builder %>
> > <% end %>
> > <div class="actions">
> > <%= f.submit %>
> > </div>
> > <% end %>
>
> > Code from views/resources/_form:
>
> > <div class="field">
> > <%= f.label :subject %><br />
> > <%= f.text_field :subject %>
> > </div>
> > <div class="field">
> > <%= f.label :course %><br />
> > <%= f.text_field :course %>
> > </div>
> > <div class="field">
> > <%= f.label :alternative_use %><br />
> > <%= f.text_field :alternative_use %>
> > </div>
> > <div class="field">
> > <%= f.label :author %><br />
> > <%= f.text_field :author %>
> > </div>
> > <div class="field">
> > <%= f.label :resource_type %><br />
> > <%= f.text_field :resource_type %>
> > </div>
>
> > Code from models:
>
> >     class University < ActiveRecord::Base
> > has_many :resources
> > accepts_nested_attributes_for :resources, :allow_destroy => :true,
> >     :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
> > attr_accessible :resource_attributes
> > end
>
> > class Resource < ActiveRecord::Base
> > #Defines the relationship between resource and user.
> > belongs_to :user
> > belongs_to :university
> > attr_accessible :resource_type, :subject, :author, :course,
> > :alternative_use,
> > #Need to add some more validation to the fields.
> > def self.search(search)
> >   if search
> >     find(:all, :conditions => ['subject LIKE ?', "%#{search}%"])
> >   else
> >     find(:all)
> >   end
> > end
> > #End class.
> > end
>
> > Thanks in advance,
> > Jen!
>
> > On 17/07/11 22:27, Barney wrote:
> >> Hello,
> >>       An earlier post where I asked how to put mulitple tables's input
> >> on one screen was answered with "accepts_nested_attributes_for" and
> >> that seems to be what I need, but nothing appears on the screen for
> >> the nested section.  I've been using ideas from:
>
> >>http://masonoise.wordpress.com/2010/07/23/rails-and-forms-using-accep...
>
> >>http://railscasts.com/episodes/196-nested-model-form-part-1
> >>http://asciicasts.com/episodes/196-nested-model-form-part-1
> >>http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/Clas...
>
> >> The following are my classes and a little code from the
> >> "_form.html.erb" that I'm trying to make work:
>
> >> class Person<  ActiveRecord::Base
> >>     default_scope :order =>  'last_name'
> >>     has_many :people_skills
>
> >>     accepts_nested_attributes_for :people_skills, :update_only =>
> >> true, :allow_destroy =>  true
> >> end
>
> >> class PeopleSkill<  ActiveRecord::Base
> >>     belongs_to :person
> >> end
>
> >> Also, there is a field in the people_skills table that is person_id,
> >> which I remember reading in the "Agile.." book creates an FK there.
>
> >> <%= form_for(@person) do |f| %>
> >> <% if @person.errors.any? %>
> >>      # ... usual code concerning errors
> >> <% end %>
>
> >> <%= f.fields_for :people_skills do |builder| %>
> >> <%= builder.label :skill, "Skill" %><br />
> >> <%= builder.text_field :skill %>
> >> </p>
> >> <p>
> >> <%= builder.label :competency, "Competency" %><br />
> >> <%= builder.text_field :competency %>
> >> </p>
> >> <% end %>
>
> >> <table>
> >> <tr>
> >> <td>
> >> <div class="field">
> >> <%= f.label :first_name %><br />
> >> <%= f.text_field :first_name %>
> >> </div>
> >> </td>
> >> <td>
> >> AND THEN MANY OTHER ROWS AND FIELDS OF THIS TABLE.
>
> >> The table data for the class "Person" shows up fine, but anything with
> >> the "f.fields_for..." is invisible (but doesn't cause a syntax
>
> >> error).  What DOES cause an error is to put
> >> "@Person.people_skills.build" in the "new" method of
> >> people_controller.rb.
>
> >> So, I'm looking for a way to have one screen which creates entries in
> >> 2 tables, linked by the PK-FK.  What am I doing wrong or forgetting to
> >> do?
> >>       Thanks,
> >>            Barney
>
> > \

-- 
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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to