Sent from my iPad
On Jul 19, 2011, at 6:33 PM, Jen <jen.bot...@gmail.com> wrote: > I forgot to mention that currently each of my tables is handled by a separate > controller. > > I thought this would be ok and anything entered in my resources _form partial > would be handled by the 'resources' controller etc. However 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 not rendering because the > universities table and resources have their own totally separate controllers? > > 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| %> You have to make sure that a resource has been built for it to show the fields, otherwise the fields_for call will not display the form fields as there is not a model Instance behind the fields. This is usually done by scoping a build through the association. For example if a university has a has_many association named resources then somewhere in the controller or view you would have to make a call similar to: @university.resources.build. This will create a new instance of the resources model to be used by fields for. >> <%= 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-accepts_nested_attributes_for/ >>> >>> 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/ClassMethods.html#method-i-accepts_nested_attributes_for >>> >>> >>> 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. > -- 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.