Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

view_paths does not work with actions or partials

#520

I've been unable to get my view_paths working as I would expect when I use the :action and :partial options for render. It only seems to affect the :template option.

Reported by Chris Vincent · July 1st, 2008 @ 01:10 AM

State: wontfix
Milestone: 2.x
Assigned to: josh josh
Importance: none

Activity

  1. Pratik
    Pratik
    • State changed from new to incomplete

    Could you please provide a failing test case ?

    Thanks.

    July 1st, 2008 @ 12:42 PM

  2. Chris Vincent
    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
    end
    

    I 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.

    July 1st, 2008 @ 09:24 PM

  3. Chris Vincent
    Chris Vincent
    • Tag changed from actionpack, bug to actionpack, bug, tested

    By the way, these were performed with 2.0.2.

    July 1st, 2008 @ 09:25 PM

  4. Ben Marini
    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

    July 11th, 2008 @ 07:24 PM

  5. Ben Marini
    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
    
    

    July 11th, 2008 @ 07:27 PM

  6. Chris Vincent
    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.

    July 11th, 2008 @ 07:46 PM

  7. josh
    josh
    • State changed from incomplete to wontfix

    December 15th, 2008 @ 09:48 PM

  8. Tiago Bastos
    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.

    http://apidock.com/rails/ActionV...

    January 22nd, 2009 @ 06:19 AM