我有照片模型和房间模型,我正在使用带有活动管理员的回形针宝石。在照片模型中,我有两列“image”和“smallimage”。出于性能原因,我想在房间索引视图中使用“smallimage”,并在房间显示视图中使用“图像”。使用activeadmin一切都很完美,但我有问题要调用正确的列。
一切都只与图像列完美配合。我已经做了很多事情,但不知何故,我不能调用正确的列,并始终得到错误的说法。如果您需要更多信息,请告诉我。任何提示都非常赞赏!
1)如何只在索引(房间)视图中调用smallimage?
2)如何只在节目(房间)视图中调用图像列?
index.html.erb
<div>
<% @rooms.where(:active => true).each do |room| %>
<%= link_to room_path(room) do %>
<div><%= image_tag room.photos[0].image.url(:thumb) if room.photos.length > 0 %></div>
<% end %>
<% end %>
</div>
show.html.erb
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<% if @photos %>
<ol class="carousel-indicators">
<% @photos.each do |photo| %>
<li data-target="#myCarousel" data-slide-to="<%= photo.id %>"></li>
<% end %>
</ol>
<% end %>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<% if @photos %>
<% @photos.each do |photo| %>
<div class="item <%= 'active' if photo.id == @photos[0].id %>">
<%= image_tag photo.image.url() %>
</div>
<% end %>
<% end %>
</div>
...
</div>
rooms_controller.rb
before_action :set_room, only: [:show]
def index
sort = params[:sort]
sort = nil unless sort.in?(['address', 'listing_name', 'nightly_price', 'nightly_price desc', 'reviews'])
@rooms = Room.order(sort).paginate(page: params[:page], per_page: 12)
end
def show
@photos = @room.photos
@booked = Reservation.where("room_id = ? AND user_id = ?", @room.id, current_user.id).present? if current_user
@reviews = @room.reviews
@hasReview = @reviews.find_by(user_id: current_user.id) if current_user
end
private
def set_room
@room = Room.find(params[:id])
end
schema.rb
create_table "photos", force: :cascade do |t|
t.integer "room_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "smallimage_file_name"
t.string "smallimage_content_type"
t.integer "smallimage_file_size"
t.datetime "smallimage_updated_at"
t.index ["room_id"], name: "index_photos_on_room_id", using: :btree
end
create_table "rooms", force: :cascade do |t|
t.string "listing_name"
t.string "accommodation_type"
t.integer "persons"
t.integer "property"
t.integer "living_area"
t.text "rooms_total"
t.text "features_short"
t.string "pets"
t.string "smoking"
t.string "check_in"
t.string "check_out"
t.string "location"
t.text "nearby_distance"
t.text "features_long"
t.text "detailed_description"
t.text "house_rules"
t.string "address"
t.text "video"
t.integer "nightly_price"
t.integer "hourly_price"
t.text "detailed_price"
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "latitude"
t.float "longitude"
end
add_foreign_key "photos", "rooms"