简体   繁体   中英

How to dynamically control text_field property in rails form

I would like to set a rails form text_field property to read only when the model already contains a value.

Here is my code

<%= f.text_field :domain, class="span4", readonly: "true" %>

I want to avoid doing something verbose like:

<%if @model.domain.empty? %>
   <%= f.text_field :domain, class="span4"%>
<%else%>
   <%= f.text_field :domain, class="span4", readonly: "true" %>
<%end%>

Thanks!

<%= f.text_field :domain, class="span4", :disabled => !@model.domain.blank? %>

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