Simple_form: Can't change the default button class.

Created on 22 Sep 2015  路  9Comments  路  Source: heartcombo/simple_form

Hi guys
I just newbie in simple_form.
I use simple_form and bootstrap at same time
I use rails generate simple_form:install --bootstrap to generate all files.
Now I want to use btn-primary as my f.button style. but I can't override the default class that is btn btn-default.
I know I can change config/initializers/simple_form_bootstrap this file. Did I have some other solution just in my code area?

Most helpful comment

The simple_form wrapper around the rails button helper actually does not do too much (just the different API and adding SimpleForm.button_class to the list of classes). It aliases the original button helper to button_button. So you can just use the original rails button or submit helper:

<%= f.button_button t(".submit"), name: "submit", class: "btn btn-primary" %>
or 
<%= f.submit t(".submit"), class: "btn btn-primary" %>

All 9 comments

I think btn btn-default btn-primary works, but if it doesn't, you can just remove btn-default from the config and add it where it makes sense.

There's no way to switch a default class as far as I remember. Hope that helps, also please make sure to use the mailing list or stack overflow for questions. Thanks!

Thank you for answer my question
And one more little question
So why the button didn't support input_html like other f.input's

Our button implementation is just a simple wrapper around the Rails submit helper, so html options are all handled by it. Since there's no concept of a "wrapper" around the button as the inputs have, it didn't seem necessary to add such options. Hope that helps :).

It is tricky. I hope there is a way to temperately override the current button default class, while still keep original default button class as is.

The simple_form wrapper around the rails button helper actually does not do too much (just the different API and adding SimpleForm.button_class to the list of classes). It aliases the original button helper to button_button. So you can just use the original rails button or submit helper:

<%= f.button_button t(".submit"), name: "submit", class: "btn btn-primary" %>
or 
<%= f.submit t(".submit"), class: "btn btn-primary" %>

Hello, I don't know If this thread is still active but I think behaviour of adding class to button is misleading. How I understand the comment # Default class for buttons is that class specified in config.button_class = 'btn' will be used on every button(that is correct). But if i specify class by myself like
<%= f.button :submit, t('submit'), class: 'btn btn-secondary' %>, I expect that default class will be overwritten by specified class and not merged together like in code

Isn't this how It should behave? Thank you.

Hi, I don't know if this thread is active. I think the simplest solution is to use the good old <input> tag like %input.btn.btn-success{type: 'submit', name: 'commit',value: "#{t('.submit')}"}.
It's simple straight

proposal:
leave class as it is (adding classes) and add 2 virtual keys just for this purpose: :add_class and :replace_class.
I'm still looking for a workaround to get btn-success to work, it does in Bootstrap 3 it doesn't now in Bootstrap 4, that doesn't involve replacing all buttons...

In your simple form config put something like:
config.button_class = 'btn simple-form-btn'

Now, with css you can target this button:

.simple-form-btn:not([class*='btn-']) {
  @extend .btn-primary;
}

Now, if the button does NOT have any other class starting with btn-, it will be shown as btn-primary. If I do add a class like btn-success, it doesnt extend .btn-primary and the btn-success class will be applied correctly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gmrash picture gmrash  路  3Comments

sarunw picture sarunw  路  4Comments

JanStevens picture JanStevens  路  5Comments

Naokimi picture Naokimi  路  4Comments

yshmarov picture yshmarov  路  4Comments