Hello everyone, i wonder if someone can help i’m a newcomer and this
problem is driving me crazy.
I have this two models with a habtm relationship Entidad(entity) and
Persona(person), on the people view i want to see the entity/es he
belongs to and viceversa, so far so good.
The thing im getting stuck is with the form that handles the editing to
add more people to an entity, heres what i got (mostly scaffold code
with some additions):
In the controller:
def edit
@persona = Persona.find(params[:id])
@persona_entidad = Persona.find(params[:id]).entidades
@entidades = Entidad.find(:all, :order => “nombre”).map {|u|
[u.nombre, u.id] }
end
def update
@persona = Persona.find(params[:id])
@persona.entidades << @entidades
@persona.save
if @persona.update_attributes(params[:persona])
flash[:notice] = ‘Persona was successfully updated.’
redirect_to :action => ‘show’, :id => @persona
else
render :action => ‘edit’
end
end
On the view:
Añadir pertenencia a
entidad
<%= select(:entidades_personas, :entidad_id, @entidades ) %>
I get th following error:
Entidad expected, got NilClass
The closer i’ve got to the solution is with this change to the
controller:
def update
@persona = Persona.find(params[:id])
@persona.entidades << Entidad.find(params[:entidad_id])
@persona.save
if @persona.update_attributes(params[:persona])
flash[:notice] = ‘Persona was successfully updated.’
redirect_to :action => ‘show’, :id => @persona
else
render :action => ‘edit’
end
end
where i get this error: Couldn’t find Entidad without an ID
Basically i want an object of the type Entidad to be added to
Persona.entidad, i can do it with irb but im unable with the rails
models.
Thanks in advance and bye everyone