Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
agregando validaciones de nota
  • Loading branch information
unknown authored and unknown committed Apr 12, 2012
1 parent 771175d commit 94d2f94
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
8 changes: 4 additions & 4 deletions app/controllers/cursos_controller.rb
Expand Up @@ -65,6 +65,7 @@ def create
def update
@curso = Curso.find(params[:id])


respond_to do |format|
if @curso.update_attributes(params[:curso])
format.html { redirect_to @curso, notice: 'Curso was successfully updated.' }
Expand All @@ -91,9 +92,6 @@ def destroy
def ingresar_notas
if params[:id]
@curso = Curso.find(params[:id])
puts "holaaa" +@curso.formula
#else
# @curso = Curso.find(1)
end
@cursos = Curso.all

Expand All @@ -105,7 +103,6 @@ def ingresar_notas

def resultado
cursoFormula = params[:formula]
puts "FORMULA2 "+ cursoFormula
elementos = cursoFormula.split('+')
promedio = 0.0
faltante = 0
Expand All @@ -130,6 +127,7 @@ def resultado
respond_to do |format|
format.html # show.html.erb
end

end


Expand All @@ -140,4 +138,6 @@ def authenticate
end
end



end
35 changes: 33 additions & 2 deletions app/models/curso.rb
@@ -1,7 +1,38 @@
class Curso < ActiveRecord::Base

validates_presence_of :nombre, :message => "Ingrese un nombre"
validates_presence_of :formula, :message => "Ingrese la formula"
validates_presence_of :nombre, :message => "no ha sido ingresado."
validates_presence_of :formula, :message => "no ha sido ingresada."

after_validation :validateFormula

def validateFormula

acumulado = 0

formulaPlus = self[:formula].split("+")

for i in 0..formulaPlus.length-1
pesoXExam = formulaPlus[i].scan(/\d\d%[A-Z0-9]+/)
if pesoXExam.length == 0
errors.add(:formula," es incorrecta.")
return false
end
end

pesos = self[:formula].scan(/\d\d/)
for i in 0..pesos.length-1
acumulado = acumulado + pesos[i].to_i
end

if acumulado == 100

return true
else
errors.add(:formula," no llega al 100%.")

# raise "ERROR PS MUY TARDE"
return false
end

end
end
13 changes: 11 additions & 2 deletions app/views/cursos/_form.html.erb
@@ -1,7 +1,16 @@
<%= form_for(@curso) do |f| %>
<% if @curso.errors.any? %>
<% if @curso.errors.any?
cantidadErrores = @curso.errors.count
mensajeError=""
if cantidadErrores ==1
mensajeError = "Se presentó " + cantidadErrores.to_s+ " error al grabar el curso."
else
mensajeError = "Se presentaron " + cantidadErrores.to_s+ " errrores al grabar el curso."
end
%>

<div id="error_explanation">
<h2><%= pluralize(@curso.errors.count, "error") %> prohibited this curso from being saved:</h2>
<h2><%= mensajeError %> </h2>

<ul>
<% @curso.errors.full_messages.each do |msg| %>
Expand Down
21 changes: 20 additions & 1 deletion app/views/cursos/ingresar_notas.html.erb
Expand Up @@ -54,6 +54,25 @@
$("#resultado").submit();
}


function isNumberKey(evt,obj)
{
var charCode = (evt.which) ? evt.which : event.keyCode
var nota = (obj.value!="")?obj.value:0;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else
{

notaCheck = ""+nota + (parseInt(charCode) - 48);
if (parseInt(notaCheck)<0 || parseInt(notaCheck) >20 ){
return false;

}
return true;
}

}
</script>
<form accept-charset="UTF-8" action="/ingresar_notas" id="resultado" method="post">
<%if @curso != nil%>
Expand Down Expand Up @@ -94,7 +113,7 @@
%>
<div style="display:none" id="div<%=elementoTemp[1]%>" data-role="fieldcontain" >
<label for="<%=elementoTemp[1]%>"><%=elementoTemp[1]%></label>
<input type="text" name="<%=elementoTemp[1]%>" id="<%=elementoTemp[1]%>" value="" />
<input type="text" name="<%=elementoTemp[1]%>" id="<%=elementoTemp[1]%>" value="" onKeypress="return isNumberKey(event,this)"/>
</div>
<script type"text/javascript">
camposForm[<%=i%>] = "<%=elementoTemp[1]%>" ;
Expand Down

0 comments on commit 94d2f94

Please sign in to comment.