undefined method `each' for nil:NilClass spree app - ruby-on-rails

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

Related

Spree active menu

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>

How to avoid same instance variables for footer

I have a footer in my rails application that shows all recent posts and categories produced by two instances variables in the welcome_controller.rb
But then if i go to another section that has a different controller responsible i get an error as the #posts and #categories instance variables are not there to loop through and list the latest posts and categories.
welcome_controller.rb (code extract)
#categories = Category.all
#posts = Post.order("created_at DESC")
_footer.html
<div class="row footer-black">
<div class="large-3 columns text-left">
<h4>Categories</h4>
<ul>
<% #categories.each do |c| %>
<% unless c.header? %>
<% if c.restriction %>
<% if check_for_free_questions_in_restricted_category(c) %>
<li> <%= link_to c.title, category_random_free_question_path(c),'data-no-turbolink' => true %> </li>
<% else %>
<li> <%= link_to c.title, new_subscription_path,'data-no-turbolink' => true %> </li>
<% end %>
<% else %>
<li> <%= link_to c.title, category_random_question_path(c),'data-no-turbolink' => true %> </li>
<% end %>
<% end %>
<% end %>
</ul>
</div>
<div class="large-5 columns text-left">
<h4>Latest Posts</h4>
<ul>
<% #posts.each do |p| %>
<li> <%= link_to truncate(p.title, length:70), blog_post_path(p) %> </li>
<% end %>
</ul>
</div>
<div class="large-4 columns text-left social-links">
<h4>Social</h4>
<a href="#">
<i class="fa fa-facebook-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-twitter-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-google-plus-square fa-2x"></i>
</a>
</div>
</div>
is there a way to show categories and posts in footer without instantiating same variables over and over again?
The short answer is 'no'. Every variable must be instantiated (initialized) before it can be used (in this case, in your view).
Now, there are some more efficient ways of instantiating these variables than writing:
#categories = Category.all
#posts = Post.order("created_at DESC")
in multiple controllers. But, that's a different question. If you're interested in that, then ask a new question or refine this one.

Rails view returning database field after loop

In my view I do this:
<ul class="list-group">
<%= JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
It lists everything out like it should, but then at the end it returns the value of current_printer.stripe_managed_account.fields_needed. I'm not sure why it does this, or how to prevent it from doing this.
This is a screenshot:
Replace <%= %>, on your each line by <% %>.
Because <%= #your each %> is same that : <% puts #your each %>.
Try this:
<ul class="list-group">
<% JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
Thanks #vee

Devise User Profile link in Navbar

I have the following code for the nav-bar in my application, using Devise for user authentication.
<ul class="nav pull-right">
<li class="">
<%= link_to root_path do %>
<i class="icon-home"></i> Home
<% end %>
</li>
<li class="">
<%= link_to current_user do %>
<i class="icon-book"></i> Portfolio
<% end %>
</li>
<li class="">
<%= link_to help_path do %>
<i class="icon-question-sign"></i> Help
<% end %>
</li>
<li class="">
<%= link_to(destroy_user_session_path, :method=>'delete') do %>
<i class="icon-signout"></i> Logout
<% end %>
</li>
</ul>
It works fine on the home page and the user profile page, but when I go to the other pages, such as the Projects or Help page( both of which have before_filter :authenticate_user! in their controllers ), it throws the following error
NoMethodError in Projects#index
undefined method `find_by_remember_token' for #<Class:0x3ec18d0>
It says that the offending code is this part:
<%= link_to current_user do %>
Which I use to link the user to his/her profile. Any ideas how to fix this error?
Update:
It seems like the problem solved itself after adding
#user = current_user
to the page's respective controllers.
Try to check if user signed in.
<%= link_to if_user_signed_in?, current_user %>

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