Evan
Evan

Reputation: 355

Disable a Text Box in Ruby on Rails?

I have the code:

        <% generate_bullets = Bullet.all %>
        <% generate_bullets.shuffle.first(4).each do |t| %>
        <%= f.text_field, :bullets, :class => 'text_field disabled' %>

I want to disable a text box using embedded ruby, and am unable to do so. If I could receive any help on the situation I'm facing, it would be very greatly appreciated.

After I disable the text box I want to have a button generate four random ID's from the database table "bullets" and print them on the disabled text box in an array format, and utilize those four printed ID's to post them onto a created page. Any help with that would be even better.

Upvotes: 15

Views: 19528

Answers (2)

Azmat Rana
Azmat Rana

Reputation: 562

You can also use :readonly => true attribute.

For HAML

= f.text_field :name, :class => "form-control", :readonly => true

For ERB

<%= f.text_field :name, :class => "form-control", :readonly => true %>

Upvotes: 6

Matchu
Matchu

Reputation: 85792

Let me know if I'm reading this right: you're trying to disable the text field from the get-go in the HTML. Is that right?

If so, disabled isn't a class; it's its own attribute.

<%= f.text_field, :bullets, :class => 'text_field', :disabled => true %>

Upvotes: 34

Related Questions