vvofdcx
vvofdcx

Reputation: 519

Ruby on Rails: how to set size of number_field

Could someone show me how to set the size of number_field in rails? I tried this but it doesn't work:

<%= f.number_field :status, size: "10", ...

but this works:

<%= f.text_field :name, size: "10", ...

Thanks

Upvotes: 16

Views: 19254

Answers (4)

MDickten
MDickten

Reputation: 156

As of 2023 (and Rails 5.2, and Chrome), I've found setting max doesn't work (for me).

However, it's simply possible to set the width by using the style attribute:

<%= number_field_tag :count, 20, style: "width:70px;" %>

(Or, as a possibly cleaner solution, give it a CSS class that sets this width.)

Upvotes: 0

gsumk
gsumk

Reputation: 901

I was using number_field_tag and I had to add empty braces for second parameter when I was not using value.

number_field_tag(name, value = nil, options = {})

This is what documentation says and I did not use value so my code with max looks like this

<%= number_field_tag :name,{}, max: @max_amount %>

Upvotes: 1

hobbydev
hobbydev

Reputation: 1719

<%= form.number_field :n, {min: 0, max: 99} %>

This tag has not any syntax error.

Upvotes: 1

Arup Rakshit
Arup Rakshit

Reputation: 118289

You need to use :max option.

<%= f.number_field :name, max: 10, .. %>

Read the documentation number_field_tag.

number_field_tag(name, value = nil, options = {})

Creates a number field.

Options

  • :min - The minimum acceptable value.

  • :max - The maximum acceptable value.

  • :in - A range specifying the :min and :max values.

  • :step - The acceptable value granularity.

Upvotes: 22

Related Questions