Ruby On Rails:使文本字段不可编辑,并在编辑记录时显示另一个字段 [英] Ruby On Rails: make text field non-editable and display another field while editing a record

查看:50
本文介绍了Ruby On Rails:使文本字段不可编辑,并在编辑记录时显示另一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.如何在编辑记录时使文本字段不可编辑(可读=假)

1.How can i make a text field non-editable (readable=false) while editing record

我的表单查看代码

  <div class="field">
    <%= f.label t :profile_name %><br />
    <%= f.text_field :profile_name %>
  </div>

2.如何在编辑记录时使字段可见.我在数据库表中有一个字段,我只想在编辑记录时显示该字段,但是在创建新记录时该字段不可见.

2.How can i make a field visible while editing a record. I have a field in the database table that i just want to display to fill in while editing the record however while create a new record that field not be visible.

已解决:

没关系的人,我得到了解决方案,

Never Mind Guys i got the solution,

1. 
<div class="field">
  <%= f.label t :profile_name %><br />
  <%= f.text_field :profile_name, :readonly => f.object.persisted? %>
</div>

2. 
<% if f.object.persisted?%>
  <div class="field">
    <%= f.label t :another_field %><br />
    <%= f.text_field :another_field%>
  </div>
<% end %>

推荐答案

如果您希望文本框不可编辑,则可以使用少量CSS或在前端进行如下操作:

If you want your textbox to non-editable, you can do with little css or in the front end like the following:

 <div class="field">
    <%= f.label t :profile_name %><br />
    <%= f.text_field :profile_name, disabled: disabled %>
 </div>

或添加:readonly =>true ,而不是 disabled .所以,

or add :readonly => true instead of disabled. so,

<div class="field">
    <%= f.label t :profile_name %><br />
    <%= f.text_field :profile_name, readonly: true %>
 </div>

在视图中添加禁用"以使文本框不可编辑.如果它不起作用,我们也可以在css中进行操作.请让我知道它是否有效.

Add disabled in your view to make a non-editable text box. We can do in css as well if its not work. Just let me know if its worked or not.

这篇关于Ruby On Rails:使文本字段不可编辑,并在编辑记录时显示另一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆