Refuse partial from helper_method

So, I have a helper method in the application controller:

def run_test(test_name) #computation stuff render :partial => test_name end 

And I call it that in the views:

 <%= run_test("testpartial") %> 

and it does ok with only 1 (though ... partial rendering seems to return an array, not just partial content?), but if I put the run_test helper call on the view twice, I get a double render error that shouldn't happen with partial.

Any ideas?

+6
source share
3 answers

render in the controller compared to render , different methods are used in the view. Ultimately, the controller calls render in the view, but the controller's render method itself expects to be called only once. It looks like this:

 # Check for double render errors and set the content_type after rendering. def render(*args) #:nodoc: raise ::AbstractController::DoubleRenderError if response_body super self.content_type ||= Mime[formats.first].to_s response_body end 

Notice how it rises if called more than once?

When you call helper_method , you give the proxy a view of the version of the render controller, which is not intended to be used in the same way as the ActionView , which , unlike the controller, it is expected that it will be called repeating to do partial and something else.

+6
source

This seems to work in Rails 3.2:

 # application_helper.rb def render_my_partial render "my_partial" end 
+5
source

You can try using the render_to_string method in a view helper

 render_to_string :partial => test_name, :layout => false 
-2
source

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


All Articles