简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM