管理图片@视图

时间:2009-08-06 04:33:10

标签: ruby-on-rails

我的代码如下:

<% if @photos.blank? %>
  No Pic
<% else %>    
<center>  
    <table>
      <tr>
    <% @photos.each do |c|  %>
      <td>
<%= image_tag(url_for({:action => 'image', :id => c.id}),:width =>'20%', :height =>'20%' ) %><br>
    <%= link_to "lihat" , {:action => 'show2', :id => c.id} -%>
        <% if logged_in? %>
        <small> <%= link_to 'Edit', {:action => "edit2", :id => c.id } %></small>
        <small> <%= link_to 'Delete', {:action => "delete2", :id => c.id }, :confirm => "apakah anda yakin mau mengapus berita ini?" %></small>
        <% end %>
        </td>

        <% end %>
            </tr>

    </table>
 </center>

 <% end %>

我想打印我的图像“if column = 5”每行自动添加
或5个图像 输出: [图像] [图像] [图像] [图像] [图像]

我的代码怎么办?请帮帮我

6 个答案:

答案 0 :(得分:3)

有几种方法可以解决“连续5张照片”的问题,但我认为这是最具表现力的:group照片数组为5行并使用嵌套{{ 1}}循环:

.each

请注意,如果最后一行没有5张照片,rows = @photos.in_groups_of(5) rows.each do |row| <tr> row.each do |photo| <td> #display photo here </td> end </tr> end 将使用array#in_groups_of填充照片,除非您指定其他默认设置。

答案 1 :(得分:1)

你可以做这样的事情

@photos.each_slice(5) do |slice|
   slice.each do |photo|
     # render photo.name, photo.title...etc here
   end
end

答案 2 :(得分:0)

if i modified with slice :

<h1>All Photos</h1>

<% @photos.each_slice(5) do |slice|  -%>
    <%  slice.each do %>

<h2><%= link_to photo.title,
photo_path(:user_id => photo.user, :id => photo) %></h2>
<p>

        <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
<br>
<% if is_logged_in? and logged_in_user.id == @user.id -%>


<%= link_to 'Destroy', photo_path(:user_id => photo.user, :id => photo),
:confirm => 'Are you sure?', :method => :delete %>
<% end %>

<% end -%>
</p>
<% end -%>

错误:

显示第5行引发的照片/ index.html.erb:

的未定义方法`title'

提取的来源(第5行):

2: 
3: <% @photos.each_slice(5) do |slice|  -%>
4:  <%  slice.each do %>
5: <h2><%= link_to slice.title,
6: photo_path(:user_id => photo.user, :id => photo) %></h2>
7: <p>
8: 

我尝试修改:

<% rows = @photos.in_groups_of(5)%>
<% rows.each do |row| %> 
  <tr>
  <% row.each do |photo| %>
    <td>
    <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
    </td>
  <% end %>
  </tr>
<% end %>

错误:

RuntimeError in Photos#index

显示第6行引发的照片/ index.html.erb:

调用id为nil,错误地为4 - 如果你真的想要id为nil,请使用object_id

提取的来源(第6行):

3:   <tr>
4:   <% row.each do |photo| %>
5:     <td>
6:      <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
7:     </td>
8:   <% end %>
9:   </tr>

我错了吗?

答案 3 :(得分:0)

看起来你遇到了我在原始答案中提到的问题:

“请注意,如果您的最后一行没有5张照片,array#in_groups_of将使用nil填充照片,除非您指定其他默认设置。”

试试这个:

<tr>
<% row.each do |photo| %>
  <td>
    <% if photo %>
      <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
    <% end %>
  </td>
<% end %>
</tr>

答案 4 :(得分:0)

我被修改了:

<% rows = @photos.in_groups_of(5)%>
<% rows.each do |row| %> 
  <tr>
  <% row.each do |photo| %>
    <td>
        <% if photo do %>
    <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
        <% end %>
    </td>
  <% end %>
  </tr>
<% end %>

结果:

SyntaxError in Photos#index

Showing photos/index.html.erb where line #15 raised:

compile error
D:/Rails/rails_apps/pameran_teknologi/app/views/photos/index.html.erb:15: syntax error, unexpected $end, expecting kEND

Extracted source (around line #15):

12: <% end %>
13: 

but if i modify like this :

<% row.each do |photo| %>
    <td>
        <% if photo %> // without 'do'
    <%= image_tag(url_for({:action => 'image', :id => photo.id}),:width => '10%',:height => '10%' ) %>
        <% end %>
    </td>
  <% end %>

结果:

alt text http://kuyarails.jimdo.com/s/cc_images/cache_1636419413.jpg?t=1249628156

答案 5 :(得分:0)

我在这里稍微更改了代码

slice.each do |photo|
   # photo.name, photo.title...
end