Paul L. wrote:
On 24 Sep 2006, at 21:13, simplydope wrote:
NoMethodError in Department#create
Showing app/views/department/_form.rhtml where line #34 raised:
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each
…
34: <% @companies.each do |company| %>
Seems to me that you don’t have an @companies instance variable in
whichever view includes _form. So is there a path in your department
controller that can use the _form, without first initialising
@companies?
Paul
No exactly sure what you’re asking…but here is my
department_controller.rb
class DepartmentController < ApplicationController
def index
list
render :action => ‘list’
end
GETs should be safe (see
URIs, Addressability, and the use of HTTP GET and POST)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def list
@department_pages, @departments = paginate :departments, :per_page
=> 20,
:conditions => “deleted_yn = 0”, :order_by => “dept_name”
end
def show
@department = Department.find(params[:id])
end
def new
@department = Department.new
@companies = Company.find(:all, :conditions => “deleted_yn = 0”,
:order => “comp_name”)
@regions = Region.find(:all, :conditions => “deleted_yn = 0”, :order
=> “region”)
end
def create
@department = Department.new(params[:department])
if @department.save
flash[:notice] = ‘Department was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
def edit
@department = Department.find(params[:id])
@companies = Company.find_all
@regions = Region.find_all
end
def update
@department = Department.find(params[:id])
if @department.update_attributes(params[:department])
flash[:notice] = ‘Department was successfully updated.’
redirect_to :action => ‘list’
else
render :action => ‘edit’
end
end
def destroy
@department = Department.find(params[:id])
Department.update(params[:id], {:deleted_yn => 1})
redirect_to :action => ‘list’
end
end
Thanks,
George