返回顶部

收藏

Display random image

更多
File: app\controllers\hello_controller.rb

class HelloController < ApplicationController
  def show
            @images  = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg']
            @random_no = rand(5)
            @random_image = @images[@random_no]
    end

end

File: app\views\hello\show.rhtml

<html>
<head>
<title>Random Image</title>
</head>
<body>
<h1>Random Image</h1>

Displaying image <%= @random_image%>

<img src='/public/images/<%= @random_image%>'>

</body>
</html>

标签:Ruby,Rails

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 张映 发表 2013-01-31 16:33:11 centos ruby on rails apache mysql 安装配置详解
  2. sonataservice 发表 2013-07-04 09:17:04 [上海] 招 Ruby On Rails Developer 两枚
  3. hisea 发表 2013-07-25 16:32:48 [7.25]免费 Kindle 电子书: Ruby on Rails 4.0 Guide
  4. lan_chi 发表 2013-06-12 06:33:40 ruby2.x+rails4.x+puppet
  5. Alex Wood 发表 2014-12-10 17:20:18 Using Amazon RDS with Ruby on Rails and AWS OpsWor
  6. techug 发表 2014-12-30 15:35:42 你不可能有超过10年的Rails编程经验!
  7. 博主 发表 2011-09-16 16:44:00 Load Rails Environment with Ruby 1.9 Nearly as Fas
  8. Erick Guan 发表 2015-03-08 18:52:01 Ensuring the file uploaded by Refile
  9. viviworld 发表 2015-05-04 08:31:32 Ruby 和 Python
  10. jiyaping 发表 2011-04-09 00:00:00 搭建ruby on rails 环境
  11. 博主 发表 2005-11-04 08:00:00 many things inspired by ruby on rails
  12. 博主 发表 2012-09-23 00:00:00 Ruby On Rails环境搭建

发表评论