Rails:如何在表单的必填字段上禁用星号?

问题描述 投票:36回答:11

当我添加'Required'属性时 对于html输入字段,Rails在标签之前预先添加星号(*)。

有谁知道如何防止这种情况?

出于某种原因,Rails转换了这个:

<%= f.input :Company, :input_html => {:value => "", :id => "company_name"}, :label => "company name" %>

进入这个:

<div class="input string required">
    <label for="company_name" class="string required">
    <abbr title="required">*</abbr> company name</label>
    <input type="text" value="" size="50" required="required" name="lead[Company]" id="company_name" class="string required">
</div>

我不喜欢它在DIV中包装所有内容并向聚会添加ABBR元素。

我怎么能阻止这个?

ruby-on-rails ruby-on-rails-3.1 simple-form
11个回答
33
投票

在config / initializers / simple_form.rb中添加以下行:

config.label_text = lambda { |label, required| "#{label}" }

0
投票

您可以使用form_for,并在def label中覆盖方法config/initializer,为必填字段添加星号,如下所示:

def label(object_name, method, content_or_options = nil, options = nil, &block)
    if content_or_options.is_a?(Hash)

      content_or_options.each do |key, val|
        options[key] = val
      end

      content_or_options = method.to_s
    end

    content_or_options ||= method.to_s

    presence_validations = [ActiveModel::Validations::PresenceValidator, ActiveRecord::Validations::PresenceValidator]

    class_obj = options[:object].class if options[:object]
    class_obj ||= object_name.to_s.camelize.constantize

    validations = class_obj.validators_on(method.to_s).map(&:class)

    if (presence_validations.map { |pv| validations.include?(pv) }).any?
      content_or_options += "*"
    end

    Tags::Label.new(object_name, method, self, content_or_options, options).render(&block)
end

如果您使用普通的form_for,并使用validates_presence_of,此方法会在所有必填字段后面添加星号


0
投票

您可以从整个表单中删除它:

<%= simple_form_for @form, defaults: { required: false } do |f| %>

47
投票

您可以在simple_form的语言环境文件中将所需标记设置为空值:

en:
  simple_form:
    required:
      text: 'required'
      mark: '*'

或者使用CSS来隐藏它。


20
投票

我正在使用Rails 3.1,对于给定的模型,我在_form.html.erb中有以下视图代码:

<div>
  <%= f.label :full_name %><br/>
  <%= f.text_field :full_name, :required => true %><br/>
</div>

如果你这样做,标签不会显示星号。除非您发布代码,否则我无法确定您的方法是什么,以及我的解决方案是否适合所述方法。

更新的答案:听起来你从某人那里继承了这段代码。无论如何,在阅读完代码示例后,您肯定会使用simple_form gem。有关该宝石的信息可以在这里找到https://github.com/plataformatec/simple_form。要回答您的问题,如果您更改以下代码:

<%= f.input :Company, :input_html => {:value => "", :id => "company_name"}, :label => "company name", :required => false %>

这应该关闭星号。

我想补充一点,基于你对simple_form生成的HTML的厌恶,听起来你应该放弃使用Rails默认表单助手来重新编写你的表单代码,可以在这里阅读http://guides.rubyonrails.org/form_helpers.html。根据代码库的大小,你可能最好只是吮吸它并学习如何使用simple_form gem来节省时间,但如果你认为你有时间改变它,那就去吧。


15
投票

最简单的方法是用这个css隐藏它:

abbr[title="required"] {
  display: none;
}

5
投票

根本不是铁轨。这是simple_form宝石。因此,如果您不希望所有包装元素都不使用simple_form。使用Rails形式helpers。它比定制你不喜欢的东西更简单。


3
投票

对于使用Formtastic并遇到此问题的任何人,您可以通过编辑配置文件来删除星号,该文件通常是app / config / initializers / formtastic.rb。

改变这一行:# Formtastic::SemanticFormBuilder.required_string = "(required)"

是:Formtastic::SemanticFormBuilder.required_string = ""

更多信息here


3
投票

帮助我解决星号问题的代码:

abbr[title="required"] {
  display: none;
}

选择的答案和其他要求更改locales文件中的HTML的建议dint帮助我使用最新的Simple_form gem。


2
投票

除了在接受的答案中建议的全局配置,您可以将required: false作为输入选项传递,或者defaults: { required: false }将其设置为整个表单。


0
投票

我发现如果你只想删除它后面的星号(*)那么你所要做的就是去找这个文件file /config/locales/simple_form.en.yml

再一次不是一个很好的做法来改变宝石的配置文件和你出于某种原因使用的东西,它总是一个问题为什么你真的使用simple_form!

但是,例如我发现了这一点,因为我们使用的simple_form有很多好处,但现在是一个更好的可用性实践,在所有字段上都没有星号,然后是必需的字段。

© www.soinside.com 2019 - 2024. All rights reserved.