【问题标题】:Randomly output text from array to view - RAILS [closed]从数组中随机输出文本到视图 - RAILS [关闭]
【发布时间】:2013-01-17 16:18:15
【问题描述】:

从数组中随机输出文本到视图 - RAILS

【问题讨论】:

    标签: ruby-on-rails arrays output


    【解决方案1】:

    这不是一个格式正确的问题......你应该更准确地了解你想要什么,你正在使用什么等等。

    假设您使用 Rails 3,ERB 生成 HTML:

    # 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 %>
    

    【讨论】:

    • 您可以在阵列上使用#sample
    • @PittacusLore 也许如果您给我们更多内容而不是一句话问题标题,我们可以提供帮助。我们可以看到代码吗?你遇到什么错误?你试过什么?你在用什么? ERB、HAM​​L?什么 Ruby 和什么 Rails 版本?等等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多