Rails Render Partial in Helper

Rendering partials from a helper method

You need to pass f to addRound

def addRound(f)
render partial: "add_round", locals: { f: f }
end

and in the view

<%= addRound(f) %>

How to properly call 'render' on a partial, within a helper method?

As discussed in the linked Q&A, you should be able to do:

def render_appropriate_flyout_images_for(tag)
if tag.child_tags.present?
tag.child_tags.map(&:images).flatten.uniq.each do |image|
concat render partial: "tasks/bottom_panel_flyout_image", locals: { image: image }
end
else
tag.images.each do |image|
concat render partial: "tasks/bottom_panel_flyout_image", locals: { image: image }
end
end
end

To reduce repetition a bit, you also might try:

def render_appropriate_flyout_images_for(tag)
images = tag.child_tags.present? ? tag.child_tags.map(&:images).flatten.uniq : tag.images
images.each do |image|
concat render partial: "tasks/bottom_panel_flyout_image", locals: { image: image }
end
end

Rails Render Partial in Helper

Actually, html_safe should be used like this:-

<%= display_replies(reply).html_safe %>

Render partial from Helper in Rails 4

I think your until loop should be a while loop. Since i_projects and i_courses are both initialized to zero and n will always be greater than or equal to zero, your loop only executes when n is zero.

UPDATE

The problem is that your helper method isn't returning the rendered partials. It is rendering them, as confirmed by your logs, but the result never gets returned. I'm sure there is a better way to do it, but here is a solution:

def storico_studente
n_training_courses = get_training_courses
n_training_projects = get_training_projects
n = n_training_courses + n_training_projects
i_projects = 0
i_courses = 0

partials = []

while i_courses + i_projects < n
if @training_course_student[i_courses].nil?
partials << render(partial: 'students/training_project', collection: @training_course_student[i_projects])
i_projects += 1 if i_projects < n_training_projects
elsif @training_projects_student[i_projects].nil?
partials << render(partial: 'students/training_course', collection: @training_course_student[i_courses])
i_courses += 1 if i_courses < n_training_courses
elsif @training_course_student[i_courses].data_inizio < @training_projects_student[i_projects].data_inizio
partials << render(partial: 'students/training_course', collection: @training_course_student[i_courses])
i_courses += 1 if i_courses < n_training_courses
else
partials << render(partial: 'students/training_project', collection: @training_course_student[i_projects])
i_projects += 1 if i_projects < n_training_projects
end
end

safe_join partials
end

Render partial from helper_method

render in a controller versus render in a view are different methods. The controller eventually calls render on a 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

Note how it raises if called more than once?

When you call helper_method you give the view a proxy to the controller's version of render, which is not intended to be used in the same way as ActionView's, which is, unlike the controller's, expected to be called repeated to render partials and whatnot.

Using Rails helpers to render partials

Rendering a partial doesn't belong in a helper. Helpers should help you to do things that contain logic. Logic doesn't belong in the controller unless it's logic to render partials and decide if something should be displayed or not.

Render partial to String in view helper

You can just use render. My demo:

In my view:

<% foo = render 'foo_thing', bar:"formal bar" %>
<%= foo %>

In _foo_thing.html.erb:

<%= "This bar is a #{bar}" %>

And on my screen:

 This bar is a formal bar 


Related Topics



Leave a reply



Submit