This project is archived and is in readonly mode.
view_paths does not work with actions or partials
-
Chris Vincent
Okay. I managed to test for render :action but I'm not sure how to test that a specific partial got rendered. Here's the code:
### app/controllers/view_paths_test_controller.rb class ViewPathsTestController < ApplicationController before_filter do prepend_view_path 'views/overrides' end def action_without_override render :action => 'action_without_override' end def action_with_override render :action => 'action_with_override' end def partial_without_override render :action => 'partial_without_override' end def partial_with_override render :action => 'partial_with_override' end end ### test/functional/view_paths_test_controller_test.rb require File.dirname(__FILE__) + '/../test_helper' class ViewPathsTestControllerTest < ActionController::TestCase def setup @controller = ViewPathsTestController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end def test_action_without_override get :action_without_override assert_template 'view_paths_test/action_without_override' end def test_action_with_override get :action_with_override assert_template 'overrides/action_with_override' end endI have the following templates:
- views/view_paths_test/action_without_override.html.erb
- views/view_paths_test/action_with_override.html.erb
- views/view_paths_test/partial_without_override.html.erb
- views/view_paths_test/_partial_without_override.html.erb
- views/view_paths_test/partial_with_override.html.erb
- views/view_paths_test/_partial_with_override.html.erb
- views/overrides/action_with_override.html.erb
- views/overrides/_partial_with_override.html.erb
The first test passes, the second fails since view_paths_test/action_with_override.html.erb is rendered instead of overrides/action_with_override.html.erb. Again, I'm not sure how to assert that a partial got rendered so I don't know how to write tests for that, although all the actual code and templates are in place for demonstration.
-
Chris Vincent
- Tag changed from actionpack, bug to actionpack, bug, tested
By the way, these were performed with 2.0.2.
-
Ben Marini
I tested out your code in Rails 2.1 and had the same result.
A couple problems with your test though:
prepend_view_path adds a new base path, so you would need:
views/overrides/view_paths_test/action_with_override.html.erb
instead of
views/overrides/action_with_override.html.erb
I was able to get #prepend_view_paths to work as expected by making the following changes to your code example:
My templates:
- views/view_paths_test/action_without_override.html.erb
- views/view_paths_test/action_with_override.html.erb
- views/view_paths_test/partial_without_override.html.erb
- views/view_paths_test/_partial_without_override.html.erb
- views/view_paths_test/partial_with_override.html.erb
- views/view_paths_test/_partial_with_override.html.erb
- other_views/view_paths_test/action_with_override.html.erb
- other_views/view_paths_test/_partial_with_override.html.erb
-
Ben Marini
Whoops, didn't like my formatting:
class ViewPathsTestController < ApplicationController # Doesn't work when using block form! before_filter :override_views def override_views self.prepend_view_path RAILS_ROOT + '/app/other_views' end -
Chris Vincent
Aha, that clears it up! The docs don't explain this very well. The examples provided are:
ArticleController.prepend_view_path("views/default") ArticleController.prepend_view_path(["views/default", "views/custom"])Also, is there a method for asserting that a partial was rendered? It would be reassuring to have tests for the partials as well.
-
josh
- State changed from incomplete to wontfix
-
Tiago Bastos
- Assigned user set to josh
Marini don't use prepend_view_path with a before_filter in Rails 2.2, that will use a lot of your file system (stat, stat64) couse will re-index the paths all time (every request).
Try to save a Path object in some place and use-it instead of Strings. Other way is like the code says, preload at start-up.