mi problema es el siguiente. tengo dos tablas, regions y pais,
relacionadas
de uno a mucho respectivamente.
mis models son los siguientes
class Region < ActiveRecord::Base
has_many :pais,
:dependent => :delete_all
validates_presence_of :nombre
validates_uniqueness_of :nombre
end
class Pai < ActiveRecord::Base
belongs_to :region
validates_uniqueness_of :nombre
validates_uniqueness_of :nombre
end
tengo las vistas listas!.. con las vistas de pais, relacionadas.
el problema se viene cuando quiero validar la presencia de los campos de
pais
el models de pais me queda de la siguiente manera
class Pai < ActiveRecord::Base
belongs_to :region
validates_presence_of :nombre
validates_uniqueness_of :nombre
validates_uniqueness_of :nombre
end
antes de esto podia hacer todo sin problema…
despues de agregar
validates_presence_of :nombre
me lanza el siguiente error:
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
Extracted source (around line #10):
7:
Region:
8:
9:
10: <% @regions.each do |region| %>
11: <option value=“<%=
region.id %>”
12: <%= ’ selected’ if region.id == @pai.region_id %>>
13: <%= region.nombre %>
he buscado en google. y me he dado cuenta de que no soy el unico con el
problema.
en los controller tengo lo siguiente:
class PaisController < ApplicationController
def index
list
render :action => ‘list’
end
GETs should be safe (see
http://www.w3.org/2001/tag/doc/whenToUseGet.html
)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def list
@pai_pages, @pais = paginate :pais, :per_page => 10
end
def show
@pai = Pai.find(params[:id])
end
def new
@pai = Pai.new
@regions = Region.find_all
end
def create
@pai = Pai.new(params[:pai])
if @pai.save
flash[:notice] = ‘Pai was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
def edit
@pai = Pai.find(params[:id])
@regions = Region.find_all
end
def update
@pai = Pai.find(params[:id])
if @pai.update_attributes(params[:pai])
flash[:notice] = ‘Pai was successfully updated.’
redirect_to :action => ‘show’, :id => @pai
else
render :action => ‘edit’
end
end
def destroy
Pai.find(params[:id]).destroy
redirect_to :action => ‘list’
end
end
class RegionsController < 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
@region_pages, @regions = paginate :regions, :per_page => 10
end
def show
@region = Region.find(params[:id])
end
def new
@region = Region.new
end
def create
@region = Region.new(params[:region])
if @region.save
flash[:notice] = ‘Region was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
def edit
@region = Region.find(params[:id])
end
def update
@region = Region.find (params[:id])
if @region.update_attributes(params[:region])
flash[:notice] = ‘Region was successfully updated.’
redirect_to :action => ‘show’, :id => @region
else
render :action => ‘edit’
end
end
def destroy
Region.find(params[:id]).destroy
redirect_to :action => ‘list’
end
end
antes de haber colocado la validacion, no tenia problemas… podia
agregar y
editar sin problemas. despues de aver puesto la validacion. lanzo el
error
que envie.
…