0

I have a helper file which includes a method which contains the following snippet:

header_id = Nokogiri::HTML.parse((render "shopfront/headers_#{@current_user.shop.header_id}"))

What I'm doing is getting the result of the partial contents (which contains some ERB), and then parsing it with Nokogiri for further use.

I'm trying to determine if there is a way to do this without using render, as I would like to parse another view file in the same way, in the same method -- and at the moment it works perfectly (to parse the executed ERB in the view file), however I run into a double render issue.

Render and/or redirect were called multiple times in this action

Any tips as to how to achieve it without using a render, or making it possible to use a render twice in the same method without running into this issue (as I'm not actually rendering the view at all)?

Chris
  • 438
  • 4
  • 17

1 Answers1

1

The best bay is to logicaly get the values. If you really want to parse the view you have generated in your app, try following

rendered_html = ApplicationController.render(
  template: "/shopfront/headers_#{@current_user.shop.header_id}",
  locals: {
    :@var => "value"
  }
)

Then

Nokogiri::HTML.parse(rendered_html)
dileep nandanam
  • 2,827
  • 18
  • 20
  • Thanks! Just figured this out myself, so good to see it validated. FYI I used `assigns` instead of `locals` - also added a `layout: false` to only get the partial. – Chris Mar 06 '21 at 11:53