开发者

Ruby on Rails: How would I add a link from a custom plugin to my app's navigation?

I'm trying to add a navigation link from a plugin that I made without altering the main app files. For example, say that I have this:

app/views/shared/_navigation.html.erb

<ul id="navigation开发者_开发百科">
 <li><a href="#">Nav link A</a></li>
 <li><a href="#">Nav link B</a></li>
 <li><a href="#">Nav link C</a></li>
</ul>

If I have a custom plugin called "recipes" in my vendor/plugins file and I wanted to add this:

<li><a href="link_to_recipes">Recipes</a></li>

to the _navigation.html.erb file mentioned above (after the "Nav link C" link), what would I have to do?


I believe that you can provide hooks in the plugin's init.rb file, and you can also install or register new items in the install.rb file, but I can't seem to find the info on how to have a plugin add a link to an existing _navigation.html.erb file.

I am thinking I have to alter the navigation file to something like this:

<ul id="navigation">
 <li><a href="#">Nav link A</a></li>
 <li><a href="#">Nav link B</a></li>
 <li><a href="#">Nav link C</a></li>
 <div id="links_from_plugins">
   <!-- links from plugins will go here -->
 </div>
</ul>

..and then add links from the plugin's init.rb to the #links_from_plugins id somehow.

Any ideas?


You need to provide more information. From the examples in question shows, there's no need to even bother with a plugin. In fact there's nothing in your question that depends on any information from the plugin.


That said, you do have some legitimate questions about plugins. The way that plugins generally work, is that that you define your new methods and classes in modules, then include those modules in the core Rails classes that that they belong.

The general structure of a plugin goes like this:

plugin/
  - Readme
  - init.rb
  - install.rb
  - test/
    - unit and functional tests
  - lib/
    - plugin source files
  - generators/
    - any generators you want to provide 

In your case, you've probably done something like the following. Which under proper plugin structure goes in a file like plugin/lib/recipes.rb

module Recipes
  def link_to_recipe 
    ..
  end
end

In the plugins init.rb, you would want to do this to add your new method to those available in views:

 require 'lib/recipes.rb'
 ActionView::Base.send(:include, Recipies)

Rails will have access to all generators from the plugin's directory and you should be using the init.rb(which is loaded when Rails starts automatically) to load the plugin libraries and hook it into the core class. install.rb is used to do any tasks your plugin needs to do after installation. Its execution is triggered after your user installs the plugin with script/plugin install. install.rb should be used to copy assets over, pretty much things that belong in the public directory. That your plugin depends on. However, If you're using engines, than your plugin is a miniature Rails app and you will not have to copy assets.

Out of curiosity, are you going to use this recipe module in more than one project? If not you shouldn't go to the extra trouble of making a plugin. Instead the code belongs in a related helper module, or RAILS_ROOT/lib


You will have to alter the navigation template, one way or the other. There needs to be a line there saying: “this is where I want additional links to appear.”

A method outside a view that generates output for the view is always a Helper, in my humble opinion. So I would have the plug-in define a RecipesHelper module. Then, from init.rb you reopen the ActionView::Helpers module and include your RecipesHelper.

Finally, your view would look like:

<ul id="navigation">
 <li><a href="#">Nav link A</a></li>
 <li><a href="#">Nav link B</a></li>
 <li><a href="#">Nav link C</a></li>
 <li><%= link_to_recipes %></li>
</ul>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜