Rails Custom Plugin/Gem with Partials
I am writing a gem which provides helpers for views. The HTML I want to insert via the helper is complex enough that I'd rather write it in a _partial.html.erb file. How do I get the gem's view path include in the application's load_path?
Note: the only gem I've found that does something like this is Devise. When a view cannot be found, Rails prints the load path which (on my machine) looks like:
Missing partial sortable_nested_set/tree with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/jason/VirtualRestaurant3/app/views", "/home/jason/.rvm/gems/ruby-1.9.2-preview3/gems/devise-1.1.rc0/app/views"
How does Devise do it?
My gem: http://github.com/jrmurad/SortableNested开发者_运维技巧Set Devise gem: +http://+github.com/plataformatec/devise
If you put the 'app' directory in the base directory of your plugin, Rails will add app/views to the view path by default.
.
├── app
│ └── views
│ └── plugin_name
│ └── _my_partial.html.erb
Then you can render the partial from your plugin's helper method with:
render :partial => "plugin_name/my_partial"
Note that Rails doesn't make the views available by default in the plugin test environment. To Include them there add the following to test/test_helper.rb:
ActionController::Base.prepend_view_path File.dirname(__FILE__) + "/../app/views"
精彩评论