ruby on rails - Bootstrap keep current tab after error message -
i on rails 4 using bootstrap tabs navigate through user profile (devise). keep current tab open if error message displayed.
for example, when user submits new password, enters wrong current password, devise error message show up, switches first tab.
i have gotten current tab stay active on refresh this:
<script> $(document).ready(function() { if(location.hash) { $('a[href=' + location.hash + ']').tab('show'); } $(document.body).on("click", "a[data-toggle]", function(event) { location.hash = this.getattribute("href"); }); }); $(window).on('popstate', function() { var anchor = location.hash || $("a[data-toggle=tab]").first().attr("href"); $('a[href=' + anchor + ']').tab('show'); }); </script> but not when error message present. here tab setup...
<div class="row"> <div class="col-md-8 col-md-offset-2"> <ul class="nav nav-tabs" style="margin-top: 80px" id="mytab"> <li class="active"><a data-toggle="tab" href="#sectiona">your answers</a></li> <li><a data-toggle="tab" href="#sectionb">change password</a></li> <li><a data-toggle="tab" href="#sectionc">edit profile</a></li> <li><a data-toggle="tab" href="#sectiond">administration</a></li> </ul> <div class="tab-content"> <div id="sectiona" class="tab-pane fade in active"> <% if @subarticles.blank? %> <br><p><strong>you have not answered questions yet. <%= link_to "click here", articlecreation_path, :onclick=>"window.open(this.href,'create_company', 'height=700, width=700');return false;" %> learn how.</strong></p> <% else %> <div style="margin-top:10px; margin-bottom:50px"> <table class="table table-striped"> <thead> <tr> <th>your answers!</th> <th>upvotes</th> <th>downvotes</th> <th>view answer</th> <th>delete answer</th> </tr> </thead> <tbody> <% @subarticles.each |subarticle| %> <tr> <td><font size="4" ><%= subarticle.article.title.camelize %></font></td> <td><%= subarticle.get_upvotes.size %></td> <td><%= subarticle.get_downvotes.size %></td> <td><%=link_to 'view',article_subarticle_path(subarticle.article, subarticle),class:"btn btn-default"%></td> <td><%=link_to 'delete', article_subarticle_path(subarticle.article_id, subarticle.id), class:"btn btn-danger",method: :delete, data: { confirm: 'are sure?' }%></td> </tr> <% end %> </tbody> </table> </div> <%end%> </div> <div id="sectionb" class="tab-pane fade"> <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :role => 'form'}) |f| %> <h3>change password</h3> <%= devise_error_messages! %> <div class="form-group" style="margin-bottom:40px"> <%= f.label :current_password %> <%= f.password_field :current_password, :autofocus => true, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :password %> <%= f.password_field :password, :autocomplete => 'off', class: 'form-control' %> </div> <div class="form-group"> <%= f.label :password_confirmation %> <%= f.password_field :password_confirmation, class: 'form-control' %> </div> <%= f.submit 'update', :class => 'btn btn-primary' %> <%= link_to "sign out", destroy_user_session_path, :class=> 'btn btn-danger', :method => :delete %> <% end %> </div> <div id="sectionc" class="tab-pane fade"> <h3>edit profile</h3> <%= devise_error_messages! %> <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :role => 'form'}) |f| %> <p>please enter current password make changes.</p> <div class="form-group" style="margin-bottom:40px"> <%= f.label :current_password %> <%= f.password_field :current_password, :autofocus => true, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :email %> <%= f.email_field :email, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :username %> <%= f.text_field :name, class: 'form-control' %> </div> <%= f.submit 'update', :class => 'btn btn-primary' %> <%= link_to "sign out", destroy_user_session_path, :class=> 'btn btn-danger', :method => :delete %> <% end %> </div> <div id="sectiond" class="tab-pane fade"> <h3>cancel account</h3> <%= devise_error_messages! %> <p>unhappy? we'll sad see go.</p> <%= button_to "cancel account", registration_path(resource_name), :onclick => "return confirm('are positively sure want cancel?')", :method => :delete, :class => 'btn btn-danger' %> </div> </div> </div> </div> what best way go this? thank you!
Comments
Post a Comment