简体   繁体   中英

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.

对于新游戏对象是这样的,它不会显示默认值,在初始化它时你可以在控制器中设置一个值

@game = Game.new(players: 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM