Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在RubyonRails应用程序中,如何将照片添加到正确的索引中?_Ruby On Rails_Ruby_Indexing - Fatal编程技术网

Ruby on rails 在RubyonRails应用程序中,如何将照片添加到正确的索引中?

Ruby on rails 在RubyonRails应用程序中,如何将照片添加到正确的索引中?,ruby-on-rails,ruby,indexing,Ruby On Rails,Ruby,Indexing,我现在有一个RubyonRails应用程序,我可以上传一张照片和一个号码 <%= form_for(@masterpiece) do |f| %> <%= f.label :user_piece %><br> <%= f.file_field :user_piece %><br> <%= f.label :piece_number %><br> <%= f.number_field :pie

我现在有一个RubyonRails应用程序,我可以上传一张照片和一个号码

<%= form_for(@masterpiece) do |f| %>
  <%= f.label :user_piece %><br>
  <%= f.file_field :user_piece %><br>
  <%= f.label :piece_number %><br>
  <%= f.number_field :piece_number %>
  <%= f.submit %>
<% end %>
然后我猜在控制器中,我需要编写一个类似于此的if-else语句。我只举了前3个例子

  def create
    @masterpiece = Masterpiece.new(masterpiece_params)

    respond_to do |format|
      if @masterpiece.save
        index_placement = @masterpiece.determine_placement
        if index_placement == 1
          # put in the first li
        elsif index_placement == 2
          # putin the second li
        else
          # put in the 3rd li
        end
        format.html { redirect_to masterpieces_url }
      else
        format.html { render :index }
      end
    end
  end
Rails V4.1.4。 使用回形针和aws sdk上传照片作为旁注

我是否需要制作20个预先确定的li,然后将索引编号与工件编号匹配

看看你的问题,我认为所有的工件编号都是唯一的和固定的(20),所以要在网格中显示它们,你可以这样做:

a按件号订购您的杰作唱片:

@masterpieces = Masterpiece.order("piece_number") #this will order them in ascending order by the piece_number field in your table
<% @masterpieces.each_slice(5) do |slice| %>
 <ul class="clearfix">
    <% slice.each do |masterpiece| %>
      <li class="pull-left"><%=image_tag masterpiece.user_piece.url(:square) %></li>
    <% end %>
 </ul>
<% end %>
b在索引操作中,将记录切成5片并显示出来:

@masterpieces = Masterpiece.order("piece_number") #this will order them in ascending order by the piece_number field in your table
<% @masterpieces.each_slice(5) do |slice| %>
 <ul class="clearfix">
    <% slice.each do |masterpiece| %>
      <li class="pull-left"><%=image_tag masterpiece.user_piece.url(:square) %></li>
    <% end %>
 </ul>
<% end %>


在我的问题中,我想我可以更清楚地说,我需要在网格中有20个预先确定的框。这些预定框将填充默认图像。一旦用户将照片上传到其中一个网格,它将循环浏览每张照片。在这个问答中,我将只关注网格的设置

本例中的默认图像只是“foo”

我设置了一个图像URL的散列。现在,默认图像将只是“foo”,因为我不想在Stackoverflow上添加到s3存储桶的链接

然后我把这幅杰作的所有部分都翻了一遍。 在这个循环中,我将图像铲入预先确定的散列中,该散列依赖于散列的键值,该键值与工件编号直接相关

杰作_controller.rb

  def index
    image_urls = {
      1 => ["foo"],
      2 => ["foo"],
      3 => ["foo"],
      4 => ["foo"],
      5 => ["foo"],
      6 => ["foo"],
      7 => ["foo"],
      8 => ["foo"],
      9 => ["foo"],
      10 => ["foo"],
      11 => ["foo"],
      12 => ["foo"],
      13 => ["foo"],
      14 => ["foo"],
      15 => ["foo"],
      16 => ["foo"],
      17 => ["foo"],
      18 => ["foo"],
      19 => ["foo"],
      20 => ["foo"],
    }

    Masterpiece.all.each do |masterpiece|
      image_urls[masterpiece.piece_number] << masterpiece.user_piece.url(:square)
    end

    @image_urls = image_urls
  end
def索引
图像\u URL={
1=>[“foo”],
2=>[“foo”],
3=>[“foo”],
4=>[“foo”],
5=>[“foo”],
6=>[“foo”],
7=>[“foo”],
8=>[“foo”],
9=>[“foo”],
10=>[“foo”],
11=>[“foo”],
12=>[“foo”],
13=>[“foo”],
14=>[“foo”],
15=>[“foo”],
16=>[“foo”],
17=>[“foo”],
18=>[“foo”],
19=>[“foo”],
20=>[“foo”],
}
杰作。全部。每个人都做杰作|

image\u URL[杰作.工件编号]所有工件编号都是唯一的,您想根据它们的工件编号对齐它们吗?并非所有工件编号都是唯一的。V2是指你可以上传多个到每个部分,然后它将每秒循环所有上传。谢谢你的回复。我相信这会将它缩减为列,而不是行。此外,如果缺少数字(即,我输入5,但没有4),则5将填写4。这有意义吗?@Lauren只需要一些css和html的爱,使他们成为列,我已经使用引导类,所以如果你不使用引导,那么让我知道将更新我的答案与风格。对于你的第二个问题,你能解释一下为什么你只需要填写他们的位置吗?我的意思是,如果4号没有内容,那么它只会留下一个空白的容器,它将自动不显示,因为里面没有内容。另外,您能否解释一下有关v2tanks的更多信息,以帮助Mandeep。我都明白了!下面是我对Rails部分的回答,这里是另一篇文章,说明了我是如何让第二部分工作的。美好的您还应该自动执行图像URL哈希。
  <ul>
  <% @image_urls.each_slice(5) do |row| %>
    <% row.each do |_, urls| %>
      <li class="square-image" data-urls=<%= urls.to_json %>>
        <span class="user-image"><%= image_tag(urls.sample) %></span>
      </li>
    <% end %>
  <% end %>
  </ul>