LLBrettK
LLBrettK

Reputation: 113

How can I use form_for to display a text_field that cannot be edited?

I'm trying to set up a form on one of my pages that lists some info that should not be editable. My code for the page looks like this:

<%= form_for @quote do |f| %>
    <h2 class='h2Title'>Follow Up Popup</h2>
    <div class="field" id="msQuoteNumber">
        <%= f.label "Quote Number:" %>
        <%= f.text_field :quote_number %>
    </div>
<% end %>

My page looks like this:

Right now I'm using f.text_field :quote_number to fill in the field with the quote_number from my database. This however lets the user type whatever they want into that field which I don't want. Is there a different method I can use besides text_field that just simply shows the quote_number as regular text?

Upvotes: 0

Views: 502

Answers (2)

Any Moose
Any Moose

Reputation: 648

Use a label like the line above

Was:

<%= f.label "Quote Number:" %>
<%= f.text_field :quote_number %>

Change to:

 <%= f.label "Quote Number:" %>
 <%= f.label :quote_number %>

Upvotes: 0

Zohaib A. Butt
Zohaib A. Butt

Reputation: 156

To just display the input field and disable only in case of editing: <%= f.text_field :quote_number, disabled: !(@quote.new_record?) %>

In Controller, just remove the key :quote_number from permitted attributes in case if you are editing so that no one can update the quote number after changing it via Inspect Element functionality of the browser and then submitting the form.

Upvotes: 1

Related Questions