-
Notifications
You must be signed in to change notification settings - Fork 136
- The method ‘render_navigation’ is undefined when rendering the view. How can I fix this?
- Only a part of my configured navigation gets rendered, the rest is just missing. What could be the problem?
- Can I provide the navigation items dynamically (e.g. from database) instead of using a config file?
- Can I add a ‘class’ or ‘id’ attribute to the generated ul-elements?
- Can I add a ‘class’ or ‘id’ attribute to the generated li-elements?
- Can I add a custom attribute to the generated li-elements?
- Can I add a ‘class’ or ‘id’ attribute to the generated a-elements (the links itself)?
- Is it possible to place the navigation-config file in a location other than config/ ?
- Two items are selected at the same time. What am I doing wrong?
- Can I have more than one navigation-config file for a single rails app?
- Can I conditionally render links to model edits only when a record is shown?
- The :if parameter doesn’t stop my url_helpers from firing which raises an exception. How can I fix this?
- I used the controller methods ‘navigation’ and ‘current_navigation’ to explicitly set the current navigation. What happened to them in version 3.x?
- Can I just render the name of the currently active navigation item?
- I would like to use Twitter Bootstrap – how should I approach this?
If you get an error like undefined local variable or method `render_navigation' for #<ActionView::Base:0x506d080>
when rendering the view, please make sure that you have added the simple-navigation gem as a dependency. Under Rails 2.3.x, place a config.gem
statement in your environment.rb
file. Under Rails 3.x, add an entry to your Gemfile
. You will also need to restart your rails server after installing the gem.
Absolutely make sure that you’ve included the `simple-navigation` gem in your project and not `simple_navigation` which exists but is not this gem.
Only a part of my configured navigation gets rendered, the rest is just missing. What could be the problem?
Some of the earlier 2.×.x versions ignore errors that occur in the navigation config file. In case of an error the evaluation of the file just stops silently. This has been fixed in version 2.2.3, so errors are propagated correctly again. Please upgrade to the newest version to fix the problem.
Yes you can. Please refer to http://github.com/andi/simple-navigation/wiki/Dynamic-Navigation-Items for more details.
Can I add a ‘class’ or ‘id’ attribute to the generated ul-elements (when rendering the content as list)?
You can set the class, id and other html attributes of the ul-element in the config file (for any level you like):
SimpleNavigation::Configuration.run do |navigation|
navigation.items do |primary|
primary.dom_class = 'class_name'
primary.dom_id = 'id_name'
primary.item :my_primary_item, 'Home', home_path do |sub_nav|
sub_nav.dom_class = 'class_name'
sub_nav.dom_id = 'id_name'
sub_nav.item ...
end
end
end
Can I add a ‘class’ or ‘id’ attribute to the generated li-elements (when rendering the content as list)?
Yes. Just add an :html
option to the item definition:
SimpleNavigation::Configuration.run do |navigation|
navigation.items do |primary|
primary.item :my_primary_item, 'Home', home_path, :html => {:class => 'my_class', :id => 'my_id'}
end
end
Yes. Like specifying an id or class for you li-elements, just add your custom attribute as an html option to the item definition. It will be added to the li-element as a custom attribute:
SimpleNavigation::Configuration.run do |navigation|
navigation.items do |primary|
primary.item :my_primary_item, 'Home', home_path, :html => {:my_attribute => 'value'}
end
end
Yes, this is possible as of version 3.0.0. In the options of an item, you can add a :link_html key
with the options for the link, e.g.
SimpleNavigation::Configuration.run do |navigation|
navigation.items do |primary|
primary.item :my_primary_item, 'Home', home_path, :html => {:class => 'li_class'}, :link_html => {:class => 'link_class', :title => 'my_title'}
end
end
The link options will be added as attributes to the generated a-tag. Using this option for the links or breadcrumbs renderer has no effect since all the options are applied to the a-tag anyways.
Yes. For example, if you would like to have your navigation config file in config/navigations/navigation.rb
you can do this by adding an initializer to your app, let’s say config/initializers/simple_navigation.rb
, where you put the line:
SimpleNavigation.config_file_path = File.join(Rails.root, 'config', 'navigations')
Important note for Rails 3 users: don’t put the navigation files anywhere inside the app folder (they will get autoloaded by rails – you would not want that)
This could have several reasons:
If you are using explicit highlighting in the controller, please make sure call current_navigation
BEFORE any render call:
class YourController < ApplicationController
def your_action
...
current_navigation :home
render :template => 'home'
end
If you’re relying on automatic highlighting based on the navigation item’s URLs, could it be that two items share the same URL?
If you have more than one primary navigation in your application (i.e. multiple navigation contexts), you can provide a configuration-file for each of the contexts and specify the context for which you would like to render the navigation in your view. Let’s say you have a main navigation in your app and in addition to that you have another navigation in the admin-section of your site (which possibly uses another layout). You configure your main navigation in the file config/main_navigation.rb
and you have another configuration file for the admin navigation in config/admin_navigation.rb
. To render the main navigation in the view you call:
render_navigation(:context => :main)
To render the admin-navigation simply call:
render_navigation(:context => :admin)
If you do not specify a context, the plugin loads and evaluates the default config file (config/navigation.rb
).
Yes. Simply use your model to check for persistence like this:
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
end
# config/navigation.rb
if @article && @article.persisted?
primary.item :edit_article, "Edit #{@article}", edit_article_path(@article)
end
With the code above, the link will only be displayed for the `show` action since the article is persisted.
The :if
parameter doesn’t stop my url_helpers from firing which raises an exception. How can I fix this?
In the navigation config file, if you are using an url_helper to generate the URL to a resource which should only be evaluated in certain circumstances, you usually add an :if
option to the item’s definition, e.g.
primary.item :account, 'Account', account_path(@current_user), :if => Proc.new {logged_in?}
So the url should only be evaluated if the user is logged in (and a current_user instance variable is present). The problem is that the url_helper account_path
fires as soon the config file is evaluated and before the :if
param has been taken into consideration. To make this work you have to wrap your url_helper in a Proc as well, i.e.
primary.item :account, 'Account', Proc.new {account_path(@current_user)}, :if => Proc.new {logged_in?}
I used the controller methods ‘navigation’ and ‘current_navigation’ to explicitly set the current navigation. What happened to them in version 3.x?
The controller methods navigation
and current_navigation
have been removed from version 3.0 in favor of the new option :highlights_on. If you are absolutely dependent on these methods you can use them, but you have to require them explicitly:
In your rails app, add an initializer config/initializers/simple_navigation.rb
with the following content:
require 'simple_navigation/rails_controller_methods'
Yes. Use the helper method active_navigation_item_name
in your views. See Active Navigation Item Name for more details.
See the dedicated wiki page about this topic.