Forum: Ruby on Rails loops in a helper

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
734a7bce0ec28ecc63ab0bc92cfa3182?d=identicon&s=25 Pablo (Guest)
on 2007-03-09 14:30
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.
70225136eacd3d870f64e03bff678655?d=identicon&s=25 Russell Norris (Guest)
on 2007-03-09 14:53
(Received via mailing list)
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. :)

RSL
734a7bce0ec28ecc63ab0bc92cfa3182?d=identicon&s=25 Pablo (Guest)
on 2007-03-09 15:49
Russell Norris 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. :)
>
> RSL

Thanks for na anwser but this doesnt seem to work also
10c122532c00465b809dbf9dc35806a7?d=identicon&s=25 Paolo Negri (Guest)
on 2007-03-09 15:58
(Received via mailing list)
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
734a7bce0ec28ecc63ab0bc92cfa3182?d=identicon&s=25 Pablo (Guest)
on 2007-03-09 16:24
Paolo Negri 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
1f2eadfb41362800ebc2cf211b91d0f7?d=identicon&s=25 javier ramirez (Guest)
on 2007-03-09 16:42
(Received via mailing list)
> 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!!
This topic is locked and can not be replied to.