简体   繁体   中英

Rails layout not changing between actions

I'm programming a Rails application. I want to use an specific style in the whole HTML tag in the index page, but the rest of the pages should follow the application layout. So, in my controller I have:

    def index
        render layout: 'index_layout'
    end

    # All other actions follow this scheme
    def any_other_action
    end

The index layout has an specific piece of code that changes the styling:

    <!-- Index layout -->
    <html id="index_html">
    ...

    <!-- Application layout (goes normally) -->
    <html>
    ...

However, when I change from window to window through a link_to tag in Rails, the layout is not being refreshed and the view remains with the id assigned in the previous page, for example, if I go from index to any other page, the html tag has the id index_html, and vice versa. However, if I perform a full refresh (by pressing F5 key), the layout is rendered properly. I tested in Mozilla Firefox and Chrome, in order to check if this was a browser peculiar issue, but the results are the same.

My server produces this output when I request index page, after I navigate to any other page through link_to tag and then reload this new page:

    Processing by HomeController#index as HTML
     Rendering home/index.html.haml within layouts/index_layout
     Rendered home/index.html.haml within layouts/index_layout (9.9ms)
     Rendered layouts/_navbar.html.haml (13.6ms)
    Completed 200 OK in 1254ms (Views: 1201.9ms | ActiveRecord: 0.0ms)


    # Navigation with link_to tag, the index_html id is still appearing

    Started GET "/community" for 127.0.0.1 at 2018-06-09 11:45:58-0500
    Processing by HomeController#community as HTML
     Rendering home/community.html.haml within layouts/application
     Rendered home/_community_members_panel.html.haml (6.1ms)
     Rendered home/_community_companies_panel.html.haml (8.1ms)
     Rendered home/community.html.haml within layouts/application (41.5ms)
     Rendered layouts/_navbar.html.haml (10.6ms)
    Completed 200 OK in 110ms (Views: 101.0ms | ActiveRecord: 0.0ms)


    # Full refresh of the page with F5 key, the index_html id doesn't appear, as it should.

    Started GET "/community" for 127.0.0.1 at 2018-06-09 11:46:49 -0500
    Processing by HomeController#community as HTML
     Rendering home/community.html.haml within layouts/application
     Rendered home/_community_members_panel.html.haml (5.6ms)
     Rendered home/_community_companies_panel.html.haml (6.6ms)
     Rendered home/community.html.haml within layouts/application (19.4ms)
     Rendered layouts/_navbar.html.haml (8.4ms)
    Completed 200 OK in 74ms (Views: 72.7ms | ActiveRecord: 0.0ms)

Can anyone give me some lights about what could be failing? My intuition is something about cache, but I can't find any answer that can help me. My Ruby version is 2.5.1 and my rails version is 5.1.6

我认为获取Ur客户布局的最简单方法是创建一个布局,其中控制器的名称为文件夹名称,索引为index.html.erb以反映Ur的操作。

1)

before_action custom_layout, only: :index

def custom_layout
    render 'layouts/index_layout'
end

2)

layout :layout_by_resource

def layout_by_resource
   if true #just check as you want here. controller/path/actions etc...
     "index_layout"
   else
     "application"
   end
end

I post this expecting this will be helpful for anyone with the problem I presented

The issue was the turbolinks gem. It appears that when you create a link with this gem, it reloads only the body of the page. For this reason, the html tag wasn't changed when redirected through a link but it was when refreshed. All I had to do was to add in my link tags the next code:

<%= link_to about_us_path, data: {turbolinks: false} %>

And so, the layout is fully redrawn, applying the desired changes.

Thanks to all for their answers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM