How to add default value to a number_field in rails?

后端 未结 5 1458
长发绾君心
长发绾君心 2021-02-01 01:48

I\'ve created a form

<%= form_for [current_user,@product,@bid] do |f| %>
  

<%= f.number_field :bid_amount %>

<%= f.n

相关标签:
5条回答
  • 2021-02-01 02:14

    user740584 is right, the user should not be able to edit the product_id.

    If you really do need it on your form you can use a

    <%= f.hidden_field :product_id %>
    
    0 讨论(0)
  • 2021-02-01 02:25

    Just add @product.id to your field.

    In this example <%= f.number_field :product_id, @product.id %>

    0 讨论(0)
  • 2021-02-01 02:28

    I'm assuming bid belongs to product. Therefore product_id should not be on form at all for the user to see. As an id number it's meaningless and it must be set to the correct product for all bids.

    The action on the form will automatically mean it gets set correctly in the database.

    0 讨论(0)
  • 2021-02-01 02:29

    Just:

    <p><%= f.number_field :product_id, :value => @product.id %></p> 
    
    0 讨论(0)
  • 2021-02-01 02:34
     <p><%= f.number_field :product_id, :value => @product.id %></p>
    

    more details on: NumberField

    0 讨论(0)
提交回复
热议问题