简体   繁体   English

通过侧边菜单栏进行 Rails 页面导航

[英]rails page navigation through side menu bar

I am working on an application and here I have a side menu bar (image is attached here) here is the link of screenshot.我正在开发一个应用程序,这里有一个侧边菜单栏(此处附有图像),这里是屏幕截图的链接。

what I want is to navigate through this menu, actually I want this menu to be preset on every page mentioned in menu list.我想要的是浏览这个菜单,实际上我希望在菜单列表中提到的每个页面上预设这个菜单。 And also there should be focus on link on which I am currently on.而且还应该关注我目前所在的链接。 here I have attached screenshot of that too.在这里我也附上了截图。

I was thinking to repeat my side nav menu code on all the pages on menu.我想在菜单上的所有页面上重复我的侧面导航菜单代码。 But It won't active/focus the currently active link.但它不会激活/聚焦当前活动的链接。 Any better solutions?有什么更好的解决方案吗? What should I do to solve this issue?我应该怎么做才能解决这个问题?

You can put your menu in a partial.您可以将菜单放在部分菜单中。 Lets call it _sidebar_menu.html.erb让我们称之为_sidebar_menu.html.erb

Then you load it in every page OR in your application.html.erb like this ->然后你在每个页面或在你的application.html.erb加载它,就像这样 ->

<%= render partial: "layouts/sidebar_menu" %> Replace "layouts" with any folder inside which you've kept your menu file. <%= render partial: "layouts/sidebar_menu" %>用你保存菜单文件的任何文件夹替换"layouts"

So now you've the menu appearing in every page.所以现在您的菜单出现在每一页中。 You can "select" the menu or add your defined class to make it "active" by using some javascript.您可以“选择”菜单或添加您定义的类以使用一些 javascript 使其“​​活动”。 One way is to check for the url, and make the respective link active, OR you can use your rails controller and action's name to determine which link to activate.一种方法是检查 url,并使相应的链接处于活动状态,或者您可以使用 Rails 控制器和操作的名称来确定要激活哪个链接。

$(document).ready(function(){
    controller_name = "<%= params[:controller] %>"
    //The controller name will be dynamically inserted by rails
    $(".sidebar-menu li a").each(function(){
        if($(this).attr("href").toLowerCase().indexOf(controller_name) > -1){
            $(this).addClass("active")
        }
    });
});

I am assuming your url has the controller's name in it.我假设您的网址中包含控制器的名称。 You can also use your controller's action name, like <%= params[:action] %> in order to achieve it.您还可以使用控制器的操作名称,例如<%= params[:action] %>来实现它。 You can play around with it to find the solution that helps in your case.您可以使用它来找到对您的情况有帮助的解决方案。 I hope you get the idea.我希望你能明白。 Let me know if it worked.让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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