14

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.

2 Answers 2

33

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 %>
5
  • I'd like to request some patience, as I'm still a beginner.
    – Evan
    Jun 30, 2012 at 1: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, 2012 at 1: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, 2012 at 3: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, 2012 at 21:01
  • 1
    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, 2016 at 13:01
6

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 %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.