Spree active menu - ruby-on-rails

What do i want to achieve?
I'm trying to add an active-menu (highlight current page in the menu) to my spree/ruby on rails application.
What have I tried?
After doing some research I've found this question posted very helpful, but as you might have guessed the implementation of the solution I found didn't get me the result I wanted.
The solution I found was adding the following code to the:
The Code
application_helper.rb
module ApplicationHelper
def active_class(link_path)
current_page?(link_path) ? "active" : ""
end
end
routes.rb
root 'spree/home#home'
get '/specs', to: 'spree/home#specs'
get '/about', to: 'spree/home#about'
get '/purchase', to: 'spree/home#purchase'
get '/support', to: 'spree/home#support'
nav.html.erb
<li class="<%= active_class(root_path) %>">
<%= link_to "Home", root_path %>
</li>
<li class="<%= active_class(purchase_path) %>">
<%= link_to "Purchase", purchase_path %>
</li>
<li>
<%= link_to "About", purchase_path %>
</li>
<li class="<%= active_class(specs_path) %>">
<%= link_to "Technical Details", specs_path %>
</li>
<li class="<%= active_class(support_path) %>">
<%= link_to
The Error
But now, no matter what page I go to I get the following error:
NameError in Spree::Home#purchase
Showing /home/ubuntu/workspace/mystore/app/views/spree/shared/_nav_bar.html.erb where line #9 raised:
undefined local variable or method `purchase_path' for #<#<Class:0x007f35a30c6b80>:0x007f35a3642a00>
Extracted source (around line #9):
</li>
<li class="<%= active_class(purchase_path) %>">
<%= link_to "Purchase", purchase_path %>
</li>
I've tried changing several variables, but to no avail
My question is:
How do i create an active menu in Spree (since it's the root of the problem)

See the reference below. I have used activated instead of active. you can style the activated class thereafter
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right" id="link-section">
<li class="<%= 'activated' if current_page?(root_path) %>">
<a href="/">Home
<span class="sr-only">(current)</span>
</a>
</li>
<li class="<%= 'activated' if current_page?(new_user_registration_path) %>">
<%= link_to("Register", new_user_registration_path) unless user_signed_in? %>
</li>
<li class="<%= 'activated' if current_page?(pricing_path) %>">
<%= link_to("Pricing", pricing_path) %>
</li>
<li class="">
<% if current_user %>
<%= link_to("Store", shop_path(current_user.shop)) if current_user.shop.present? %>
<% end %>
</li>
<li class="<%= 'activated' if current_page?(new_user_session_path) %>">
<%= link_to("Login", new_user_session_path) unless user_signed_in? %>
</li>
<li class="<%= 'activated' if current_page?(dashboard_path) %>">
<%= link_to("Dashboard", dashboard_path) if user_signed_in? %>
</li>
<li>
</li>
</ul>
</div>

Related

How do you use pundit for partial authorization

Consider the following block of ruby on rails code:
<% unless current_user %>
<%= card title: 'Things you can do', bodyless: true do %>
<ul class="list-group list-group-flush">
<li class="list-group-item" style="background: transparent"><i class="fa fa-key"></i> MEET OTHERS LIKE YOU</li>
<li class="list-group-item" style="background: transparent"><i class="fa fa-question-circle-o"></i> ASK A QUESTION</li>
<li class="list-group-item" style="background: transparent"><i class="fa fa-users"></i> SHARE YOUR JOURNEY</li>
</u>
<% end %>
<% end %>
The "card" is shown, but only to logged in users.
Can Pundit Authorization policies be used determine which divs/partials/cards are shown to the visitor on a website?
Yes you can, if you defined a policy and then want to show some div/partial just if that policy, then you can do it like this for example
<% if policy(#post).update? %>
<%= link_to "Edit post", edit_post_path(#post) %>
<% end %>

Best way to show empty lists in Rails

I have been working to follow the best practices in Rails in order to save time in the future.
I am displaying all terms in a view which has more than 0 results.
<% #terms.each do |t| %>
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{t.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, t) %></span>
</div>
</li>
<% end %>
What is the best way to display a different view if the term count is zero? I could do something like a simple if statement in the view doing a count but that seems cluttered. Any suggestions?
How about this:
<% #terms.each do |t| %>
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{t.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, t) %></span>
</div>
</li>
<% end.empty? and begin %>
<li class="media">
<!-- nothing to see -->
</li>
<% end %>
You can also move the code to a partial.
<%= render(:partial => 'terms', :collection => #terms) || 'There are no terms' %>
The partial:
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{term.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, term) %></span>
</div>
</li>
</li>

undefined method `each' for nil:NilClass spree app

I am getting the below error when I am trying to login to spree admin at localhost:3000/admin.
undefined method `each' for nil:NilClass
<li class="dropdown ">
<% max_level = Spree::Config[:max_level_in_taxons_menu] || 1 %>
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Shop</a>
<ul class="dropdown-menu">
<% #taxonomies.each do |taxonomy| %>
<% cache [I18n.locale, taxonomy, max_level, #taxon] do %>
<% if taxonomy.name =='Categories' %>
<% taxons = taxonomy.root.children.map do |taxon| %>
<li class="dropdown ">
<%= link_to(taxon.name, seo_url(taxon), class: 'dropdown-toggle', 'data-toggle'=>"dropdown") %>
<ul class="dropdown-menu">
<li>Black</li>
<li>Green</li>
</ul>
</li>
<% end %>
<% end %>
<% end %>
<% end %>
</ul>
</li>
The above code is where it is giving me error. Which I have in header partial. It gives me proper homepage when I go to localhost:3000, but gives an error when I try to login to admin. The header which I want to have is coming properly. But unable to login to admin.
Please help

How to link to another part of another page?

I have the below as my nav, however, the index.html.erb#meetflappy is throwing up an error:
Routing Error
No route matches [GET] "/preorder/index.html.erb"
I'd like it so that when users click meetflappy tab, they are brought to the index page and specifically to the #meetflappy part. Now this works when I'm on the index page and it just links to #meetflappy but when I'm on another page i'm having trouble here. Please advise:
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="wrapper-nav">
<div class="navbar-header">
<%= link_to logo, root_path, class: 'brand' %></a>
</div>
<ul class="nav navbar-nav navbar-right">
<li class=""><%= link_to 'Meet Flappy', 'index.html.erb#meetflappy' %></li>
<li class=""><%= link_to 'Details', 'index.html.erb#details' %></li>
<li class=""><%= link_to 'FAQ', faq_path %></li>
<li class=""><%= link_to 'Flappy\'s Story', flappystory_path %></li>
<li class=""><%= link_to 'About Us', about_path %></li>
<li class=""><%= link_to 'Checkout', preorder_checkout_path %></li>
</ul>
</div>
</div>
</nav>
Change this:
<%= link_to 'Meet Flappy', 'index.html.erb#meetflappy' %>
to this:
<%= link_to 'Meet Flappy', root_path(anchor: 'meetflappy') %>

Using ruby to make a class active via helper method - error in variables

I have been having problems with my coffeescript, so instead I would like to use ruby to make my classes active in the view when clicked. I created a helper method called nav "active", and I have a variable called 'link' in the view that increments up for each step. The idea is to have params[:id] == link , and make the class active when that is the case . However, being newer to rails and ruby I am not sure how to implement this. Any help would be appreciated.
My error
undefined local variable or method `link' for #<#<Class:0x007fb21e087820>:0x007fb21e53fd18>
My Method in application helper
def nav_active(options = {})
return "active" if params[:id] == link
end
My view
<% icon= ["icon-link", "icon-bar-chart", "icon-edit", "icon-beaker","icon-link", "icon-bar-chart", "icon-edit", "icon-beaker"] %>
<% link = 0 %>
<% #step_list.each_with_index do |step, i| %>
<% case step.media_type %>
<% when 'video' %>
<% link += 1 %>
<li class="<%= nav_active %>">
<span class="glow"></span>
<a href="<%= link %>">
<i class='icon-info-sign icon-2x'></i>
<span>Video</span>
</a>
</li>
<% when 'excel' %>
<% link += 1 %>
<li class="<%= nav_active %>">
<span class="glow"></span>
<a href="<%= link %>">
<i class="<%= icon[i] %> icon-2x"></i>
<span>Step <%= i %> </span>
</a>
</li>
<% else %>
<% link += 1 %>
<li class="dark-nav <%= nav_active %> ">
<span class="glow"></span>
<a href="<%= link %>">
<i class="<%= icon[i] %> icon-2x"></i>
<span>Step <%= i %></span>
</a>
</li>
<% end %>
The helper method nav_active doesn't have access to the local variables in your view. You have to pass that in as an argument.
# Helper
def nav_active(link)
return "active" if params[:id] == link
end
This helper now accepts an argument named link. Now link will be defined in the helper method.
<% link += 1 %>
<li class="dark-nav <%= nav_active link %> ">
In the view you also must also pass in the current value of link, so the helper method can operate on it.

Resources