2014-10-02 29 views
0

在我的控制,我的定义位置的阵列,我用sample赶随机字符串之一:如何将数组中的随机字符串传递给视图?

class IndexController < ApplicationController 
    def index 
    @location = ["England", "Scotland", "Wales", "Ireland"] 
    random_location = @location.sample 
    end 
end 

不过,我不知道在我的浏览器中显示一个国家所需的代码。

+1

只需使用'<(%)= @ location.sample%>'的视图(index.html.erb)的内部。然后你就完成了。 – 2014-10-02 15:42:24

回答

1

使用Array#sample

class IndexController < ApplicationController 
    def index 
    # @location i_var will be available automatically inside the corresponding 
    # view of this action to the Index controller. 
    @location = ["England", "Scotland", "Wales", "Ireland"] 
    end 
end 

现在视图内 - index.html.erb

Country is <%= @location.sample %> 
+0

完美,谢谢。 – samgbelton 2014-10-02 15:46:55

相关问题