Awkward Awkward - 1 year ago 218
Ruby Question

text_field disabled Ruby on Rails

I have the following code to disable a text_field when a user is not an admin and it is working fine:

<div class="form-group">
<%= f.label :nome, "Genero" %>
<%= f.text_field :nome, class: "form-control", disabled: true if not is_admin? %>

But when a user is an admin the text_field just despair, anyone one know why this is happening and what I have to do?

Answer Source

Assuming that is_admin? returns true or false, you can simply do

<%= f.text_field :nome, class: "form-control", disabled: !is_admin? %>

Currently, the if condition i.e., if not is_admin? is applied on the entire text field, which results in text field disappearance when is_admin? returns true and when the condition returns false text field is displayed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download