guy, you're using f.label, so then you have to use
f.fields_for :something do |smth| smth.label :label, "description" smth.text_field ... On Jul 21, 2011, at 16:49 , Jen wrote: > Hi, > After getting nowhere I decided to follow this article: > > http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes > > This meant replicating some already existing fields in my universities/_form, > but I felt it would atleast rule out the possibility I was doing something > silly with partials. Now I am receiving the following error: > > NoMethodError in Universities#new > > Showing /home/resource_portal/website/app/views/universities/_form.html.erb > where line #26 raised: > > undefined method `fields_for' for nil:NilClass > > Extracted source (around line #26): > > 23: <%= f.label :country %><br /> > 24: <%= f.text_field :country %> > 25: </div> > 26: <%= form.fields_for :resources do |resource| %> > 27: <%= f.label :resource_type %><br /> > 28: <%= f.text_field :resource_type %> > 29: </div> > > Trace of template inclusion: app/views/universities/new.html.erb > > Rails.root: /home/resource_portal/website > Application Trace | Framework Trace | Full Trace > > rake-0.8.7/ruby/1.9.1/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in > `method_missing' > > My code for the models, universities controller and view is attached. Can > someone please point out what I am doing wrong? I have read many articles, > posts and have tried many variations to achieve this. Do I need to set > @resources and @universities to equal some value before rendering the form? > > Code for controller: > > class UniversitiesController < ApplicationController > #insure users are signed in before viewing these pages > before_filter :authenticate > > # GET /universities > # GET /universities.xml > def index > @universities = University.all > > respond_to do |format| > format.html # index.html.erb > format.xml { render :xml => @universities } > end > end > > # GET /universities/1 > # GET /universities/1.xml > def show > @university = University.find(params[:id]) > > respond_to do |format| > format.html # show.html.erb > format.xml { render :xml => @university } > end > end > > # GET /universities/new > # GET /universities/new.xml > def new > @university = University.new > @resource = @university.resources.build > respond_to do |format| > format.html # new.html.erb > format.xml { render :xml => @university } > > end > end > > # GET /universities/1/edit > def edit > @university = University.find(params[:id]) > end > > # POST /universities > # POST /universities.xml > def create > @university = University.new(params[:university]) > > respond_to do |format| > if @university.save > format.html { redirect_to(@university, :notice => 'University was > successfully created.') } > format.xml { render :xml => @university, :status => :created, > :location => @university } > else > format.html { render :action => "new" } > format.xml { render :xml => @university.errors, :status => > :unprocessable_entity } > end > end > end > > # PUT /universities/1 > # PUT /universities/1.xml > def update > @university = University.find(params[:id]) > > respond_to do |format| > if @university.update_attributes(params[:university]) > format.html { redirect_to(@university, :notice => 'University was > successfully updated.') } > format.xml { head :ok } > else > format.html { render :action => "edit" } > format.xml { render :xml => @university.errors, :status => > :unprocessable_entity } > end > end > end > > # DELETE /universities/1 > # DELETE /universities/1.xml > def destroy > @university = University.find(params[:id]) > @university.destroy > > respond_to do |format| > format.html { redirect_to(universities_url) } > format.xml { head :ok } > end > end > end > > Code for 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}%"]) > #Join tables here > > else > find(:all) > end > end > #End class. > end > > Code for views: > > <%= 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> > <%= form.fields_for :resources do |resource| %> > <%= f.label :resource_type %><br /> > <%= f.text_field :resource_type %> > </div> > <% end %> > <div class="actions"> > <%= f.submit %> > </div> > <% end %> > > <h1>New university</h1> > > <%= render 'form' %> > <ul> > <li> <%= link_to 'Back', universities_path %> </li> > </ul> > > Thanks in advance for any further assistance, > Jen. > On 21/07/11 12:11, Colin Law wrote: >> On 21 July 2011 11:55, Jen<jen.bot...@gmail.com> wrote: >> >> Please don't top post, it makes it difficult to follow the thread. >> Insert your reply at appropriate point(s) in previous post. Thanks. >> >>> Hi Colin, >>> I can't see anything in my controller that indicates I have set @resource to >>> nil. >>> When I go directly to the resources form it renders fine and I can add data >>> about new resources. It's just when trying to render the form as a partial >>> within the universities/_form view. >> I had missed the fact that you were in a partial. Have a look at the >> Rails Guide on Layouts and Rendering, specifically the section on >> using partials and passing values to it. >> >> If you have not already done so, also have a good look the other >> guides, it will be time well spent. >> >> >> Colin >> >>> Do I need to add something in the 'universities_controller' that gives >>> @resource a value. Perhaps '@resource = Resource.new'? >>> >>> Below is the code from the 'universities_controller' so far: >>> >>> class UniversitiesController< ApplicationController >>> #insure users are signed in before viewing these pages >>> before_filter :authenticate >>> >>> # GET /universities >>> # GET /universities.xml >>> def index >>> @universities = University.all >>> >>> respond_to do |format| >>> format.html # index.html.erb >>> format.xml { render :xml => @universities } >>> end >>> end >>> >>> # GET /universities/1 >>> # GET /universities/1.xml >>> def show >>> @university = University.find(params[:id]) >>> >>> respond_to do |format| >>> format.html # show.html.erb >>> format.xml { render :xml => @university } >>> end >>> end >>> >>> # GET /universities/new >>> # GET /universities/new.xml >>> def new >>> @university = University.new >>> >>> respond_to do |format| >>> format.html # new.html.erb >>> format.xml { render :xml => @university } >>> resource = @university.resources.build >>> >>> end >>> end >>> >>> # GET /universities/1/edit >>> def edit >>> @university = University.find(params[:id]) >>> end >>> >>> # POST /universities >>> # POST /universities.xml >>> def create >>> @university = University.new(params[:university]) >>> >>> respond_to do |format| >>> if @university.save >>> format.html { redirect_to(@university, :notice => 'University was >>> successfully created.') } >>> format.xml { render :xml => @university, :status => :created, >>> :location => @university } >>> else >>> format.html { render :action => "new" } >>> format.xml { render :xml => @university.errors, :status => >>> :unprocessable_entity } >>> end >>> end >>> end >>> >>> # PUT /universities/1 >>> # PUT /universities/1.xml >>> def update >>> @university = University.find(params[:id]) >>> >>> respond_to do |format| >>> if @university.update_attributes(params[:university]) >>> format.html { redirect_to(@university, :notice => 'University was >>> successfully updated.') } >>> format.xml { head :ok } >>> else >>> format.html { render :action => "edit" } >>> format.xml { render :xml => @university.errors, :status => >>> :unprocessable_entity } >>> end >>> end >>> end >>> >>> # DELETE /universities/1 >>> # DELETE /universities/1.xml >>> def destroy >>> @university = University.find(params[:id]) >>> @university.destroy >>> >>> respond_to do |format| >>> format.html { redirect_to(universities_url) } >>> format.xml { head :ok } >>> end >>> end >>> end >>> >>> Thanks, >>> Jen. >>> >>> -- >>> 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. -- =============================================================================== Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz =============================================================================== -- 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.