【问题标题】:simple form input field as plain text not manually editable简单的表单输入字段作为纯文本不可手动编辑
【发布时间】:2013-07-28 19:02:00
【问题描述】:

我正在使用 simple_forms gem 为我的模型创建表单。

该模型包含一些字段,这些字段是基于其他字段的 javascript 计算结果,因此我希望将它们表示为文本,而不是输入字段,以便在图形上它们看起来不可编辑并且不迷惑人。

这是我目前的看法:

- field = "time_quantity"
  %strong{:id => "#{field}_str", :class => "text-success"}
    = f.object[field]
  = f.input field, as: :hidden, input_html: {value: f.object[field]}

%strong 中的文本将值写入文本,随后的输入字段是随表单插入模型中的内容。

另外一个问题是,如果我为这些字段输入了错误的数据,我将不会在它们旁边看到任何错误通知,因为它会被隐藏。如图所示:

<strong class="text-success" id="time_quantity_str" data-original-title="" title=""></strong>
<div class="input hidden project_time_quantity field_with_errors">
  <input class="hidden input-medium" id="project_time_quantity" name="project[time_quantity]" type="hidden">
  <span class="error">can't be blank</span>
</div>

我想将其更改为:

- field = "time_quantity"
  %strong{:id => "#{field}_str", :class => "text-success"}
    = f.input field, as: :plain, input_html: {value: f.object[field]}

在视图和 javascript 中有更多的 DRY。

您认为如何实现这一目标?

谢谢,

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.2 simple-form


    【解决方案1】:

    我认为您应该使用禁用的输入,或者也可以使用标签字段。试试这样:

    <%= f.input :field_name, disabled: true, hint: 'You cannot change this field' %>
    

    <%= f.label :field_name %>
    

    希望对您有所帮助。谢谢

    【讨论】:

    • 谢谢,第一个选项可以解决它,虽然图形不是我想要的。也许我可以将该输入字段的外观更改为看起来像一个标签?你知道如何使用引导程序来获得这个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多