Display random image : Action « Rails « Ruby






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>




 








randomImage.zip( 88 k)

Related examples in the same category

1.Passing Data from an Action to a View
2.Create an Application with Two Actions
3.Link to an Action