Life4ants Life4ants - 16 days ago 8
Ruby Question

Ruby on Rails number_field default value not displaying

The default value for my text field is displaying as it should, but the number field is blank.

<%= form_for(@game) do |f| %>
<%= render 'shared/error_messages', object: f.object %>

<%= f.label :nick_name, "Nickname: " %> (optional)
<%= f.text_field :nick_name, class: 'form-control', value: "newgame" %>

<%= f.label :players, "Number of players:" %> (3-8)
<%= f.number_field :players, class: 'form-control', value:3 %>

<%= f.submit "Create Game", class: "btn btn-primary" %>
<% end %>


The value shows in the resulting source html, but is not displayed:

<label for="game_nick_name">Nickname: </label> (optional)
<input class="form-control" value="newgame" type="text" name="game[nick_name]" id="game_nick_name" />

<label for="game_players">Number of players:</label> (3-8)
<input class="form-control" value="3" type="number" name="game[players]" id="game_players" />


I tried
value: '3'
and that doesn't make a difference.
To test if it was a problem with my browser, I made a simple html file on it own with a input field:

<input type='number' class='form-control' value=3>


It display as expected, so I think it must be something with my rails app.

I don't know if this is related at all to Input value doesn't display. How is that possible?, as this is a rails app.

Answer

yes in this way for new game object it will not show a default value, in controller when you are initializing it you can set a value in it

@game = Game.new(players: 3)