in my app I have form that looks like this

= simple_form_for @user do |f|
  = f.input :name, error: false
  = f.input :surname, error: false

Is there any way to avoid this repetitions (error: false)?

有帮助吗?

解决方案

If they're all of the same type, something like this should work:

= simple_form_for @user do |f|
  - [ :name , :surname ].each do |field|
    = f.input field, error: false

If not, you could use a hash or something, instead of an array, and specify the type, as well.

It appears that simple form has the following option:

If you want to pass the same options to all inputs in the form (for example, a default class), you can use the :defaults option in simple_form_for. Specific options in input call will overwrite the defaults:

<%= simple_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f| %>
  <%= f.input :username, input_html: { class: 'special' } %>
  <%= f.input :password, input_html: { maxlength: 20 } %>
  <%= f.input :remember_me, input_html: { value: '1' } %>
  <%= f.button :submit %>
<% end %>

From https://github.com/plataformatec/simple_form

So, in your case:

= simple_form_for @user , defaults: { error: false } do |f|
  = f.input :name
  = f.input :surname

其他提示

You could loop through an array of symbols

simple_form_for @user do |f|
  [:name, :surname].each do |element|
    f.input element, error: false
  end
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top