我想将image_tag用于[数字集]而不仅仅是一个数字。 (红宝石)

时间:2017-03-31 13:01:12

标签: javascript html ruby-on-rails arrays ruby

这是一种乐透,当你按F5时,新的球出现在网站上。 我的目标是按升序显示有组织的数字球。 (我使用Cloud9) 当然,我有像 1.jpg 2.jpg 等照片...... 并将它们添加到C9(Cloud9)

的文件夹中

我认为有两种解决方案

  1. 我想重新组织"抽样编号"

  2. 图片代码到列出的数字集

  3. 这是我的试用

        @todaynum= @allnum.sample
        @todaynum1= @allnum.sample
        @todaynum2= @allnum.sample
        @todaynum3= @allnum.sample
        @todaynum4= @allnum.sample
        @todaynum5= @allnum.sample 
    
        @numimg= @todaynum+".jpg"
        @numimg1= @todaynum1+".jpg"
        @numimg2= @todaynum2+".jpg"
        @numimg3= @todaynum3+".jpg"
        @numimg4= @todaynum4+".jpg"
        @numimg5= @todaynum5+".jpg"
    
        arr = (@todaynum,@todaynum1,@todaynum2,@todaynum3,@todaynum4,@todaynum5)
        arr.sort!
    
        puts arr $array = Array.new(45)
        $array = (1..45).to_a
        @lotto_list = $array.sample(6).sort
        @lotto_list_image = $array.sample(6).sort ".jpg"
    

    html.erb文件

    <%=@lotto_list%>
    <%=image_tag lotto_list_image %>
    

    但是,它根本不起作用..

    lotto list image = ~~ something(I don't know..) ~~ ".jpg"I want to do like this.
    

    我很难回答这个问题...... 我该怎么办?

    这是我的代码

    class numbersetController < ApplicationController
      def numbers
        @allnum=["1","2","3","4","5","6","7","8","9","10","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45"]
    
        @todaynum= @allnum.sample
        @todaynum1= @allnum.sample
        @todaynum2= @allnum.sample
        @todaynum3= @allnum.sample
        @todaynum4= @allnum.sample
        @todaynum5= @allnum.sample 
    
        @numimg= @todaynum+".jpg"
        @numimg1= @todaynum1+".jpg"
        @numimg2= @todaynum2+".jpg"
        @numimg3= @todaynum3+".jpg"
        @numimg4= @todaynum4+".jpg"
        @numimg5= @todaynum5+".jpg"
    
        arr = (@todaynum,@todaynum1,@todaynum2,@todaynum3,@todaynum4,@todaynum5)
        arr.sort!
        puts arr
    
        $array = Array.new(45)
        $array = (1..45).to_a
    
        @lotto_list = $array.sample(6).sort
    
      end
    end
    

    html.erb文件

    <h1>lotto balls newbie.version</h1>
    
    <p><%=image_tag @numimg%></p> 
    <h2><%=image_tag @numimg1%></h2>
    <h3><%=image_tag @numimg2%></h3>
    <h4><%=image_tag @numimg3%></h4>
    <h5><%=image_tag @numimg4%></h5>
    <h6><%=image_tag @numimg5%></h6>
    
    <%=@todaynum%>  
    <%=@todaynum1%>
    <%=@todaynum2%>
    <%=@todaynum3%>
    <%=@todaynum4%>
    <%=@todaynum5%>
    
    <h1> ruby random number (for test) </h1>
    <%=@lotto_list%>
    

1 个答案:

答案 0 :(得分:0)

您在第二次尝试时走在正确的轨道上,只需要再次拨打map来按照您希望的方式对其进行格式化。

# note: probably want to rename the controller like so
class NumbersetController < ApplicationController
  def numbers
    @lotto_ball_images = *(1..45).to_a.sample(6).sort.map(&:to_s).map{|n| "lotto-ball-image-number#{n}.jpg"}
  end
end

然后在模板中出现类似的东西。

<h1>lotto balls </h1>

<% @lotto_ball_images.each do |src| %>
   <%= image_tag(src) %>
<% end %>