Assign rendering to a partial instance variable

In rails 4, I want to make a partial (say, footer) anywhere on the page.

In home_controller.rb, I have this inside a class:

def spree_application
 @test = render :partial => 'spree/shared/footer'
end

When I go to the index page and add:

<%= @test %>

Nothing happens. I know what I can display on an index page, but I ask if there is a way to assign a rendered link to a variable.

Thank!

Edit: I made a mistake with this question. I defined: spree_application

+4
source share
2 answers

The controller rendermethod is different from viewing. You want to execute it in the context of the view:

 @test = view_context.render 'spree/shared/footer'

render_to_string , html_safe, html- <% = @test% > .

UPDATE:

. "content_for". , . , , :

# Your application layout
<html>
  <head>
  ...
  </head>
  <body>
    yield :image
    <div id="wrapper">
      yield
    </div>
  </body>
</html>

, yield :image

<% content_for :image do %>
  <%# Everything here will be displayed outside of the wraper %>
<% end %> 
+6
+10

Source: https://habr.com/ru/post/1540338/


All Articles