Skip to content

Instantly share code, notes, and snippets.

@NouranMahmoud
Last active August 29, 2015 13:57
Show Gist options
  • Save NouranMahmoud/9583575 to your computer and use it in GitHub Desktop.
Save NouranMahmoud/9583575 to your computer and use it in GitHub Desktop.
Dynamic DropDown menus related to specific action and controller
<!DOCTYPE html>
<html>
<head>
<title>MyAuthonticationSample</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body data-controller="<%= controller.controller_name %>" data-action="<%= controller.action_name %>">
<% if user_signed_in? %>
<p>Logged in as <strong><%= current_user.email %></strong>
. and your role is : <strong>
<% if (current_user.role == nil ) %>
visitor
<% else %>
<%= current_user.role.name %>
<% end %> </strong> </p>
<br>
<%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %>
<% else %>
<%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Login", new_user_session_path, :class => 'navbar-link' %>
<% end %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
</html>
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
jQuery ->
controller = $("body").data("controller")
#console.log(controller)
action = $("body").data("action")
#console.log(action)
Menus = ( parent_tag, child_tag, action) ->
console.log("I am in menu")
if(action == 'new')
$(child_tag).parent().hide()
child = $(child_tag).html()
#console.log(child)
$(parent_tag).change ->
console.log(parent_tag.concat(' :selected'))
parent = $( parent_tag.concat(' :selected')).text()
escaped_parent = parent.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
options = $(child).filter("optgroup[label='#{escaped_parent}']").html()
#console.log(options)
if options
$(child_tag).html(options)
$(child_tag).parent().show()
else
$(child_tag).empty()
$(child_tag).parent().hide()
#alert("ChangeSelection")
Menus '#estate_country_id','#estate_city_id',action
Menus '#estate_city_id','#estate_community_id',action
Menus '#estate_community_id','#estate_sub_community_id',action
Menus '#estate_sub_community_id','#estate_property_id',action
....
<%= javascript_include_tag params[:controller] %>
.....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment