【问题标题】:How to access an instance variables id in the view如何在视图中访问实例变量 id
【发布时间】:2016-07-22 17:00:19
【问题描述】:

我正在使用carrierwave将照片上传到我的应用程序中,照片与每个位置具有“belongs_to”关系,而位置与照片具有“has_many”关系。我是我的展示页面,其中上传了位置的照片,它们通过循环正常显示。但是,在我的索引页面中,我正在遍历我的位置并希望显示该位置的照片。照片具有正确的 location_id,我只是不确定视图中访问该位置照片的语法。我想做的是显示与特定位置相关的一张照片,例如:@photo(location.id.last)。任何帮助将不胜感激,在此先感谢。

查看

<div class="row">
  <!-- featured listings gallery -->
  <% @locations.each do |location| %>
    <% if location.featured == "true" %>
      <div class="col-md-4 col-xs-6">
        <div class="thumbnail">
          <div class="overlay">
            <%= image_tag @photos, :size => '400x300', :class => 'img-responsive' %>
            <div class="effect">
              <div class="overlay-header">
                <%= location.name %>
              </div>
              <div class="location-link">
                <%= link_to "View Building", location_path(location) %>
              </div>
            </div>
          </div>
          <h3 class="display-box-caption">
            <%= truncate(location.description, length: 100) %><%= link_to 'More', location_path(location), :class => 'more-info'  %>
          </h3>
        </div>
        <div class="col-md-12 property-link-container">
          <%= link_to "View Building", location_path(location), :class=>"property-link" %>
        </div>
      </div>
    <% end %>
  <% end %>
</div>

控制器

class PagesController < ApplicationController
 def index
  @locations = Location.all
  @photos = Photo.all
end
def about

end
def faqs

end
def blog

end
def whats_included

end

结束

【问题讨论】:

  • 您可以像这样访问每个位置的一张照片:&lt;%= location.photos.last.id %&gt;(这将为您提供最后一张照片的 id),如果只是这样做&lt;%= location.photos.last %&gt;(它将为您提供照片对象本身),因为您已经定义了关联。

标签: ruby-on-rails ruby


【解决方案1】:

借助关联访问照片:

location.photos.last.photo.url

HTML

<div class="row">
  <!-- featured listings gallery -->
  <% @locations.each do |location| %>
    <% if location.featured == "true" %>
      <div class="col-md-4 col-xs-6">
        <div class="thumbnail">
          <div class="overlay">
            <!-- changing @photos to location.photos.last -->
            <% if location.photos.any? and location.photos.photo.present? %>
              <%= image_tag location.photos.last.photo.url, :size => '400x300', :class => 'img-responsive' %>
            <% end %>
            <div class="effect">
              <div class="overlay-header">
                <%= location.name %>
              </div>
              <div class="location-link">
                <%= link_to "View Building", location_path(location) %>
              </div>
            </div>
          </div>
          <h3 class="display-box-caption">
            <%= truncate(location.description, length: 100) %><%= link_to 'More', location_path(location), :class => 'more-info'  %>
          </h3>
        </div>
        <div class="col-md-12 property-link-container">
          <%= link_to "View Building", location_path(location), :class=>"property-link" %>
        </div>
      </div>
    <% end %>
  <% end %>
</div>

控制器:

def index
  @locations = Location.all
  # no need to fetch photos now
  # @photos = Photo.all
end

照片模特

class Photo < ActiveRecord::Base
 belongs_to :location
 mount_uploader :photo, PhotoUploader
end

【讨论】:

  • 这个接缝很近,它给了我图像记忆位置:<0xb542a288>
  • <0xb52ba024>
【解决方案2】:

我看到 Sahil 的帖子有所帮助。添加.includes(:photos),以更快地加载查询。以后会超级好用的。

def index
  @locations = Location.includes(:photos).all
end

【讨论】:

  • 是的,这样查询更快更好,我认为应该是:photos 而不是:photo,因为位置有很多照片。
猜你喜欢
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 2021-04-21
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多