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 %>
24: <%= f.text_field :country %>
25:
26: <%= form.fields_for :resources do |resource| %>
27: <%= f.label :resource_type %>
28: <%= f.text_field :resource_type %>
29:
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? %>
<%= pluralize(@university.errors.count, "error") %> prohibited this
university from being saved:
<% @university.errors.full_messages.each do |msg| %>
- <%= msg %>
<% end %>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :address %>
<%= f.text_field :address %>
<%= f.label :country %>
<%= f.text_field :country %>
<%= form.fields_for :resources do |resource| %>
<%= f.label :resource_type %>
<%= f.text_field :resource_type %>
<% end %>
<%= f.submit %>
<% end %>
New university
<%= render ‘form’ %>
- <%= link_to 'Back', universities_path %>
Thanks in advance for any further assistance,
Jen.