ruby on rails - How to Render Current User into Nav-Bar Button -
running:
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux] rails 4.2.5
i want render current user name nav-bar button using dropdown-menu class. replace "account info" "hi "current_user".
<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">account info <span class="caret"></span></a> <ul class="dropdown-menu"> <li> <%= link_to "edit profile", edit_user_registration_path, class: "fa fa-pencil-square-o" %></li> <li> <%= link_to "sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out" %></li> </ul> <ul class = "nav pull-right"> </ul> <% else %> <!-- <%= link_to "sign up", new_user_registration_path, class: "fa fa-sign-in" %> --> <li><%= link_to "sign in", new_user_session_path, class: "fa fa-sign-in" %></li> <% end %> </ul>
i'm using devise (3.5.3) user authentication.
you'll need use if statement work out if user signed_in (i presume method exists in devise) , show different button if user signed in:
<li class="dropdown"> <% if signed_in? %> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">hi, <%= current_user.user_name %> <span class="caret"></span></a> <% else %> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">account info <span class="caret"></span></a> <% end %> <ul class="dropdown-menu"> <li> <%= link_to "edit profile", edit_user_registration_path, class: "fa fa-pencil-square-o" %></li> <li> <%= link_to "sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out" %></li> </ul> <ul class = "nav pull-right"> </ul> <% else %> <!-- <%= link_to "sign up", new_user_registration_path, class: "fa fa-sign-in" %> --> <li><%= link_to "sign in", new_user_session_path, class: "fa fa-sign-in" %></li> <% end %> </ul>
you'll want use similar if, else, end
statement hide sign_in/sign_up buttons when current_user signed_in - won't want see that.
Comments
Post a Comment