简体   繁体   中英

Incorporating Bootstrap Form Helpers into simple_form

I am trying to implement a select menu with countries , which then filters a select menu with states once a country is selected.

I use SimpleForm and Bootstrap so the following seems to suit me perfectly: http://vincentlamanna.com/BootstrapFormHelpers/state.html

I currently use the country_select gem with SimpleForm and my code contains:

<%= f.input :country, :id => "business_country", :input_html => { :class => "span6" } %>
<%= f.input :state, :input_html => { :class => "span6" } %>

When I included the relevant js files and include the default sample code for Bootstrap Form Helpers the behaviour works as expected but I would like to include it in my SimpleForm so I can save to the database. Here is the default example code that performs correctly:

<select id="countries_states1" class="input-medium bfh-countries" data-country="US"></select>
<select class="input-medium bfh-states" data-country="countries_states1"></select>

Could anyone suggest how to implement this is SimpleForm?

Thanks.

Did you follow the instructions on the simple_form gem readme to install with Bootstrap support? https://github.com/plataformatec/simple_form#twitter-bootstrap

rails generate simple_form:install --bootstrap

I got it to work by finding the correct id of my country select (business_country as seen in my code in my original question) and adding the data-country to the input_html in SimpleForm for the state select. The code:

<%= f.input :country, :input_html => { :class => "span6 input-medium bfh-countries"} %>
<%= f.input :state, as: :select, :input_html => {:class => "span6 input-medium bfh-states", 'data-country' => ('business_country') } %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM