[英]How do I pass a random string from an array into views?
在我的控制器中,我定义了一个位置数组,并使用sample
来捕获随机字符串之一:
class IndexController < ApplicationController
def index
@location = ["England", "Scotland", "Wales", "Ireland"]
random_location = @location.sample
end
end
但是,我不知道在浏览器中显示一个国家/地区所需的代码。
使用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 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.