简体   繁体   中英

Rails 5: Rendering a partial to the application layout

Within views/layouts/application.html I have the line <%= yield %> . This line is ultimately replaced by views directly corresponding to controller actions without any problems.

In one of my controllers, I am trying to render a partial instead of the default behaviour:

def show
    @service_groups = ServiceGroup.where(deleted_at: nil)
    render partial: 'table', locals: {rows: @service_groups, headers: service_group_headers}
end

I'm using a partial in this way so that I can use the same basic table structure for various different database tables (across different controllers).

This render partial code doesn't seem to work with the <%= yield %> line in the application layout. The partial code is just rendered on its own without the surrounding layout.

  1. Why is this?
  2. How do I rectify the problem?

Please let me know if I should be handling this a different way.

Thanks.

( migrating from comments )

What if you created a show.html.erb for that controller and placed render :partial there? It should work.

Explanation: Partials are to be used from "big" views. So, they are not wrapped in the layout on purpose!

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