Rails simple_form attribute required mark (*)

拥有回忆 提交于 2019-12-03 03:41:56

问题


I am using Simple Form in my app and I'd like to remove the * to indicate an attribute is required on all of my forms (existing and those yet to be created).

I have tried to set in simple_form.rb:

  # Whether attributes are required by default (or not). Default is true.
  config.required_by_default = false

And I've tried to modify simple_form.en.yml:

   required:
      text: 'required'
      mark: ''  # <------ tried setting this to blank.

I know I can set :required => false on each field, but I want to clean up the views & set it once.


回答1:


Setting the simple_form.required.mask to '' should work if you restarted the server.

But you can remove it changing the the configuration:

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

to

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



回答2:


You can also do

simple_form_for @model, :defaults => {:required => false}



回答3:


on Rails 4 just remove the "required" word

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

so it should be

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

Validation still works!




回答4:


just

# Whether attributes are required by default (or not). Default is true. config.required_by_default = false

and restart your rails server. Then it works.



来源:https://stackoverflow.com/questions/7592536/rails-simple-form-attribute-required-mark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!