Ruby on Rails - Insert a render into an html tag

Asked

Viewed 292 times

1

How do I add a render inside an HTML tag?

For example, this works okay:

<%= render :partial => "feed/new_card", :locals => {
      :main_image => image_tag('feed/new_badge.jpg')
    } %>

<div><%= main_image %></div>

But I’d like to do something like this:

<%= render :partial => "feed/new_card", :locals => {
      :main_image => <%= render feed/new_album %>
    } %>

<div><%= main_image %></div>

is it possible? Because this way is going wrong here =/

Thank you.

1 answer

0

On your controller:

@main_image = render_to_string(partial: 'feed/new_album')

In your view:

<%= render :partial => "feed/new_card", :locals => {
  :main_image => @main_image.html_safe
} %>

But be careful render_to_string is heavier than render, if this is too used can cause slowness. Explain a little better what Oce is trying to do, because this code you showed me doesn’t make much sense and there should be a more effective way to solve your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.