Loops in a helper


#1

Hello

Why this doesnt work?

application_helper

def check_collection(collection,partial_array,msg)
if collection.empty?
msg
else
partial_array.each do |partial|
render :partial => partial
end
end
end

view

<%=check_collection(@jobs,[“job_list”, “search_job”], “No job offers”)%>

I know that helper should return only last value, so it wont return 2
partials but it doesnt even display the last partial, only elements in
an array.


#2

Have you tried render :partial => partial_name, :collection =>
partial_array? I dunno that it’ll work in a helper but it’s the way to
do
what you’re doing outside of one. Might work inside as well. :slight_smile:

RSL


#3

Russell N. wrote:

Have you tried render :partial => partial_name, :collection =>
partial_array? I dunno that it’ll work in a helper but it’s the way to
do
what you’re doing outside of one. Might work inside as well. :slight_smile:

RSL

Thanks for na anwser but this doesnt seem to work also


#4

Paolo N. wrote:

If you check what each method returns

[1, 2].each {|n| n}
=> [1, 2]

You can understand why your code is not working.

partial_array.inject(’’) { |out, partial| out << (render :partial =>
partial)}

may work but I’m not sure if is a good solution.

Paolo

This works fine! Thx


#5

If you check what each method returns

[1, 2].each {|n| n}
=> [1, 2]

You can understand why your code is not working.

partial_array.inject(’’) { |out, partial| out << (render :partial =>
partial)}

may work but I’m not sure if is a good solution.

Paolo


#6

def check_collection(collection,partial_array,msg)
if collection.empty?
msg
else
partial_array.each do |partial|
render :partial => partial
end
end
end
you can create a string (str_output for example) and then do something
like
str_output+= render_to_string (:partial=>partial)

finally put at the end of the helper “str_output”, so that will be your
return value.

regards,

javier ramirez


Estamos de estreno… si necesitas llevar el control de tus gastos
visita http://www.gastosgem.com !!Es gratis!!