Search code examples
ruby-on-rails-3template-inheritance

Template inheritance's view overriding not working in Rails3


I am trying to use template inheritance in Rails 3. I have following code in my application_controller.rb:

layout :set_layout

def set_layout
  layout_name = Club.find(1).layout.name
  prepend_view_path("views/#{layout_name}")
  layout_name
end

This does correctly set layout to be the one associated with particular club (in this case called "layout1"), but when I try to override the view by making the following folder structure:

app
-views
--home
---indes.html.erb
--layout1
---home
----index.html.erb

It catches the views/home/index.html instead of views/layout1/home/index.html.erb.

When I print the view paths as following:

<% controller.view_paths.paths.each do |t| %>
  <%= t %><br>
<% end %>

It prints the correct views/layout1 as first one, but still doesn't find the content of correct index.html.erb file.

In my layout1.html.erb located in layouts folder, I just say yield.

Have I misunderstood the concept somehow?


Solution

  • After so many hours spent inspecting this, I found the problem. I was setting view paths in function that is only used to set layout like

    layout :set_layout
    
    def set_layout
      prepend_view_paths ...
      #return the layout name
    end
    

    while I should've used before filter to set view paths in different function.

    The reason why I didn't realize this earlier was that the view paths were printed correctly in layout..