How to use the Number_Field in Ruby?


I have a problem with this type of field that is inside my form and it is that when trying to save that value inside my bd, I automatically assign a zero to this field, so it does not register the value that I want .

This is the code of my form.

      <%= f.label :calificacion, "Calificacion",:class=>"etiqueta" %>
      <br />
      <%= f.number_field :calificacion,{:value => @calificacion,:step => '0.1', :class=>"caja"} %>

Next I will put the code of my method to store.

 def create

   @titulo = params[:pelicula][:titulo];
   @calificacion = params[:pelicula][:calificacion];
   @ano = params[:pelicula][:ano];
      @pais = params[:pelicula][:pais];
   @duracion = params[:pelicula][:duracion];
   @genero = params[:pelicula][:genero];
      @estreno = params[:pelicula][:estreno];
   @fecha_estreno = params[:pelicula][:fecha_estreno];
   @recomendacion = params[:pelicula][:recomendacion];
         @sinopsis = params[:pelicula][:sinopsis];
   @nota = params[:pelicula][:nota];
   @status = params[:pelicula][:status];
   @pelicula ={
      :titulo => @titulo,
      :calificacion => @calificacion,
      :ano => @ano,
            :pais => @pais,
      :duracion => @duracion,
      :genero => @genero,
            :estreno => @estreno,
      :fecha_estreno => @fecha_estreno,
      :recomendacion => @recomendacion,
            :sinopsis => @sinopsis,
      :nota => @nota,
      redirect_to peliculas_path, :notice => "La Pelicula ha sido insertada";
      render "new";

And this is the way he inserts in my bd

The above data that is entered is due to the fact that before changing it to number_field it was a text_field

asked by David 03.08.2016 в 04:27

1 answer


Do you print with puts params[:pelicula][:calificacion] with what do you get?

When you did the migration, in your database, did you put that field as n integer or is it string (check in your schema)? If you put it as a string, fix your migration to be an integer. If your database is fine, it may be that you are trying to save a string in an integer field. Test:

@calificacion = params[:pelicula][:calificacion].to_i


  • In ruby the ; is not used when finishing the lines, just in case that more than one instruction is executed in the same line, and in general it breaks with several guides of style I think.
  • Use a single instance variable @pelicula, and in your view use @pelicula.título and etc.

    (This should be a comment, but I'm missing rep)

answered by 03.08.2016 в 20:28