Randomly output text from array to view – RAILS [closed]

It is not really well formated question … You should be more precise about what you want, what you are using, etc.

Assuming you are using Rails 3, ERB for HTML generation:

# in your controller:
file_as_array = IO.readlines('filename.txt')
random_line = rand(file_as_array.size)
@my_random_string = file_as_array[random_line]

# in your view:
<%= @my_random_string %>

Leave a Comment