I am writting an application 100% ajax with rails 3.2.8
Everything was going great until I tried to upload photos of the employees to the employee management module.
This is part of my code:
At the Controller:
class EmpleadosController < ApplicationController
respond_to :js
before_filter :require_user
def index
@empleados = Empleado.paginate( page: params[ :page ],
joins: [:user,:contacto],
select: 'empleados.id as id, empleados.user_id, empleados.contratado_el, empleados.despedido_el, empleados.nss, empleados.sueldo_base_semanal, empleados.comentarios, empleados.foto_file_name, empleados.foto_content_type, empleados.foto_file_size, empleados.foto_updated_at, users.username as username, contactos.salutacion_id as salutacion_id, contactos.id as contacto_id, contactos.nombres as nombres, contactos.apellidos as apellidos'
).sorted( params[ :sort ] )
end
def new
@empleado = Empleado.new
end
def create
@empleado = Empleado.new(params[:empleado])
@empleado.sueldo_base_semanal = params[:empleado][:sueldo_base_semanal].gsub(',','').gsub('$','').to_f
if @empleado.save
redirect_to empleados_path
else
render action: 'new'
end
end
...
Now for the view (I am using HAML):
= form_for( @empleado, remote: true, html: {multipart: true} ) do |f|
= show_me_the_errors @empleado
%table.captura
%tr
%td.etiqueta
= f.label :user_id
%td.campo
= f.select :user_id,
User.usuarios_no_asignados,
{ include_blank: true },
{ tabindex: 1 }
= image_tag 'nuevo_24.png',
style: 'vertical-align: bottom;',
id: 'empleado_nuevo_usuario'
... ( 5 more text fields ) ...
%tr
%td.etiqueta
= f.label :foto
%td.campo
= f.file_field :foto,
accept: 'image/png,image/gif,image/jpeg'
The thing is that while there is not selected file for upload, everything works fine, the command respond_to :js
works as it should be in all interactions between create
with index
and new
.
But, when you select an image, all the interactions after create
with index
and new
become HTML
completely ignoring the respond_to :js
, I mean, the form for
behaves just like if the remote: true
wasn't there
When everything works well, the URL is localhost:3000 and never changes, but when I select an image to upload, the URL after crete
becomes localhost:3000/empleados
Does any one have any clue about this? I have been trying to solve this for the last 3 dyas and failed.
Thaks in advance.