Rails事件列表无法显示图像

时间:2015-04-18 20:36:43

标签: ruby-on-rails nested paperclip

我正在尝试在rails应用程序中显示我的事件列表。我可以正确显示每个事件,但不能显示与其关联的任何图像。

enter image description here

我认为这与我按月显示事件的方式有关,我不确定如何修复。我是发展的新手,非常感谢给予的任何帮助。

活动模型

class Event < ActiveRecord::Base

  self.primary_key = :id
  extend FriendlyId
  friendly_id :name, use: :slugged

  attr_accessible :name, :location, :date, :time, :price, :phone, :website, :description, :avatar, :slug, :business_id

  has_attached_file :avatar, 
    styles: {
      thumb:    '100x100>',
      square:   '200x200#',
      medium:   '400x400>',
      full:     '800x800>'
    },
    :default_url => "/assets/avatar-event-blank.png"

  validates_attachment_size :avatar, :less_than => 2.megabytes
  validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']

  validates :name, presence: true
  validates :location, presence: true
  validates :date, presence: true

  belongs_to :business
  ...

事件控制器

这是我的控制器,我将月份和列表事件按月分组。

...
def index
  @events = Event.all(:select => "name, id, date, slug", :order => "date ASC")
  @event_months = @events.group_by { |t| t.date.beginning_of_month }

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @events }
  end
end
...

事件索引

...
<% @event_months.sort.each do |month, events| %>
  <div class="titlebar">
    <span><%=h month.strftime("%B %Y") %></span>
  </div>
  <ul class="listing-events">
    <% for event in events %>
      <li><a href="/events/<%= event.slug %>" title="<%= event.name %> on <%=event.date.strftime("%B %e, %Y") %>">
        <div class="le-image" style="background-image: url('http://cpd.typepad.com/lighthousestock/images/2007/05/22/baileys_harbor_rear_0181.jpg')">
          <div class="le-avatar">
            <%= image_tag event.business.avatar.url(:full) %>
          </div>
        </div>
        <div class="le-info">
          <div itemprop="name" class="title"><%= event.name %></div>
          <div class="date"><%= event.date.strftime("%B %e, %Y") %></div>
        </div>
      </a></li>
    <% end %>
  </ul>
<% end %>
...

1 个答案:

答案 0 :(得分:0)

在查看我的代码后解决了这个问题。我必须确保选择了我试图在控制器中使用的所有属性。

事件控制器

@events = Event.all(:select => "name, id, date, business_id, avatar_file_name, avatar_updated_at, slug", :order => "date ASC")