13

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.

Yaqub Ahmad
  • 26,916
  • 22
  • 99
  • 146
Evan
  • 335
  • 1
  • 5
  • 14

2 Answers2

30

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 %>
Matchu
  • 77,193
  • 15
  • 148
  • 158
  • I'd like to request some patience, as I'm still a beginner. – Evan Jun 30 '12 at 01:04
  • But would you be able to explain as to why I receive a syntax error on lines of code that are higher than that which exist? – Evan Jun 30 '12 at 01:29
  • 1
    @Evan: that sounds…off. A more specific example would help, though perhaps the server isn't using the latest version of the file or the line number is referring to one of those giant core Rails files? :/ – Matchu Jun 30 '12 at 03:30
  • For instance, I'll receive a syntax error on line 68, but my code (and spaces from hitting enter) only go to 65. – Evan Jun 30 '12 at 21:01
  • Be warn that disabled fields are NOT send in POST/PUT/PATCH. User readonly ( see Azmat answer) if you want field which annot be set by user but is send with form data. – Foton Sep 15 '16 at 13:01
5

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 %>
Azmat Rana
  • 472
  • 3
  • 11