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
How to Replace an Array's Element
Make Text Input Fields and Their Labels Line Up Correctly
What's the Efficient Way to Multiply Two Arrays and Get Sum of Multiplied Values in Ruby
What Are Some Examples of Using Nokogiri
How to Read from Redis Inside a Multi Block in Ruby
How to Refer a Local Gem in Ruby
Heroku and @Font-Face - Embedded Fonts Wont Display on Heroku
How to Create a Jwt for Use with Apple Music
Check If the Sum of Two Different Numbers in an Array Equal a Variable Number
Groups in a Gemfile in Rails 3
How to Associate a CSS/Sass Stylesheet to a View in Rails
Vim Syntax Highlighting for Ruby 1.9
How to Do Complex Querying with Logical Operations by Using Searchkick
Postgresql Ilike with Multiple Matches in Rails Activerecord
Add External Style Sheet in Rails Project
How to Parse a Number from a String That May Have a Leading Zero
Append Row to CSV File Ruby 1.9 CSV Lib
Determining Whether One Array Contains the Contents of Another Array in Ruby