Forum: Ruby on Rails String to Array?

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.
0d0738e29d8a3dca607bb54e240ecab7?d=identicon&s=25 Matt Lee (Guest)
on 2006-02-28 18:02
(Received via mailing list)
I've got a string of keywords "internet explorer microsoft windows
browser" - I want to output each word with a link_to a controller I've
built that searches based on a keyword.

I managed to split the string, but I don't know how then to loop over it
to output the link_tos?

matt

--
Matt Lee
Senior Technical Developer
NHS Connecting for Health

---

***************************************************************************
This e-mail is confidential and privileged. If you are not the intended
recipient please accept our apologies; please do not disclose, copy or
distribute information in this e-mail or take any action in reliance on
its
contents: to do so is strictly prohibited and may be unlawful. Please
inform us that this message has gone astray before deleting it. Thank
you
for your co-operation.
***************************************************************************
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-02-28 18:08
(Received via mailing list)
Matt Lee wrote:
> I've got a string of keywords "internet explorer microsoft windows
> browser" - I want to output each word with a link_to a controller I've
> built that searches based on a keyword.
>
> I managed to split the string, but I don't know how then to loop over it
> to output the link_tos?

str.split(' ').collect{|s| link_to(s, foo)}.join('<br />')

The collect (and inject) methods are most useful for this sort of thing.
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-02-28 18:15
(Received via mailing list)
<% input = "internet explorer microsoft windows browser" %>
<%= input.split.each {|word| link_to  word, :controller=>'controller',
:action=>'action'} %>

This should work, but hasn't been tested

_Kevin
6b655cbfb773c06803214fdf03fa8d1c?d=identicon&s=25 Tom Wilcoxen (Guest)
on 2006-02-28 18:54
(Received via mailing list)
Creating an array out of a bunch of words like that is often done like:

  %w{internet explorer microsoft windows browser}

You can assign that to a variable or just build off it:

  words =  %w{internet explorer microsoft windows browser}
  links = words.collect { |w| link_to .... }

or

  %w{internet explorer microsoft windows browser}.collect ...

Then you could either do the .join('<br />') Alex mentioned or something
like:

<ul>
<% links.each do |link|  -%>
  <%= "<li>#{link}</li>"  %>
<% end -%>
</ul>

Or whatever your needs might be.

This would be where David Black's new book, Ruby for Rails would come
in handy, or, of course, The Pickaxe.

-Tom
This topic is locked and can not be replied to.