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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7650689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:06:00  来源:igfitidea点击:

Rails: How to disable asterisk on form's required fields?

ruby-on-railsruby-on-rails-3.1simple-form

提问by vsync

When I add the 'Required' attribute
to html input fields, Rails pre-pends an asterisk (*) before the label.

当我将 'Required' 属性添加
到 html 输入字段时,Rails 在标签前预置一个星号 (*)。

Does anyone know how to prevent this?

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

For some reason Rails transforms this:

出于某种原因,Rails 对此进行了转换:

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

into this:

进入这个:

<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>

I don't like that it wraps everything in a DIV and adds an ABBR element to the party.

我不喜欢它将所有内容都包装在 DIV 中并向派对添加 ABBR 元素。

How can I prevent this?

我怎样才能防止这种情况?

采纳答案by Shane Andrade

In config/initializers/simple_form.rb add this line:

在 config/initializers/simple_form.rb 添加这一行:

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

回答by RocketR

You can just set the required mark to empty value in simple_form's locale file:

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

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

Or use CSS to hide it.

或者使用 CSS 隐藏它。

回答by Lester Peabody

I'm using Rails 3.1, and I have the following view code in my _form.html.erb for a given model:

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

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

The label does not show an asterisk if you do it this way. Unless you post code I can't be sure of what your approach is and if my solution would fit said approach.

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

Updated Answer: It sounds like you've inherited this code from someone. At any rate, after reading your code sample, you are most definitely using the simple_form gem. Information about that gem can be found here https://github.com/plataformatec/simple_form. To answer your question though, if you change your code the following:

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

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

That should turn off the asterisk.

那应该关闭星号。

I would add, based on your disgust for the HTML generated from simple_form, it sounds like you should just do away with the gem and re-write your form code using the Rails default form helpers, which can be read about here http://guides.rubyonrails.org/form_helpers.html. Depending on the size of the code base, you might be better off just sucking it up and learning how to use the simple_form gem for the sake of saving time, but if you think you have the time to change it all, go for it.

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

回答by VoA

The simplest way is to hide it with this css:

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

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

回答by lucapette

It isn't rails at all. It's the simple_formgem. So, if you don't want all the wrapping elements don't use simple_form. Use Rails form helpers. It would be more simple than customize something you don't like.

它根本不是导轨。这是simple_formgem。因此,如果您不想要所有包装元素,请不要使用 simple_form。使用 Rails 表单助手。这比定制你不喜欢的东西更简单。

回答by Aaron Gray

For anyone using Formtastic and having this issue, you can remove the asterisks by editing the config file, which is typically app/config/initializers/formtastic.rb.

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

Change this line: # Formtastic::SemanticFormBuilder.required_string = "(required)"

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

to be: Formtastic::SemanticFormBuilder.required_string = ""

成为: Formtastic::SemanticFormBuilder.required_string = ""

More info here.

更多信息在这里

回答by Cybergenie

Code that has helped me solve the asterisk issue:

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

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

The chosen answer and the other suggestions asking to change the HTML in locales file dint help me with the latest Simple_form gem.

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

回答by inopinatus

Aside from the global config suggested in the accepted answer, you can pass required: falseas an input option, or defaults: { required: false }to set it for the whole form.

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

回答by FrankfromDenmark

You can remove it from the whole form:

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

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

回答by andre.orvalho

I found out that if you only want to remove the asterisk(*) behind it then all you have to do is to go to this file file /config/locales/simple_form.en.yml

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

once again is not a good practice to change your configuration files for gems and something your using for some reason, it always a question of why do you really use simple_form!

再一次更改 gems 的配置文件和出于某种原因使用的东西不是一个好习惯,它总是一个问题,为什么你真的使用 simple_form!

But for example I found out about that because there is great things about simple_form we use but nowadays is a better usability practice to have the asterisks on none required fields then required ones.

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