学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails6.0 | 民泊サイトの構築(改良版)| 24 | 写真カルーセル表示 | for MacOSX



| 23 | アクションテキスト <<  [ホーム] >> | 25 | Googleマップ


「app\assets\images」フォルダに「blank.jpg」「icon_add.png」ファイルを保存しておいてください。


記述追加 app\helpers\application_helper.rb(13行目)

    def room_cover(room)
        if room.photos.attached?
            url_for(room.photos[0])
        else
            ActionController::Base.helpers.asset_path('blank.jpg')
        end
    end



app\helpers\application_helper.rb

module ApplicationHelper

    def avatar_url(user)
        if user.avatar.attached?
            url_for(user.avatar)
        elsif user.image?
            user.image
        else
            ActionController::Base.helpers.asset_path('icon_default_avatar.jpg')
        end
    end

    def room_cover(room)
        if room.photos.attached?
            url_for(room.photos[0])
        else
            ActionController::Base.helpers.asset_path('blank.jpg')
        end
    end

end



記述追加 app\controllers\rooms_controller.rb(26行目)

  def show
    @photos = @room.photos
  end



app\controllers\rooms_controller.rb

class RoomsController < ApplicationController

  protect_from_forgery except: [:upload_photo]
  before_action :set_room, except: [:index, :new, :create]
  before_action :authenticate_user!, except: [:show]
  before_action :is_authorised, only: [:listing, :pricing, :description, :photo_upload, :amenities, :location, :update]

  def index
     @rooms = current_user.rooms
  end

  def new
    @room = current_user.rooms.build
  end

  def create
    @room = current_user.rooms.build(room_params)
    if @room.save
      redirect_to listing_room_path(@room), notice: "保存しました。"
    else
      flash[:alert] = "問題が発生しました。"
      render :new
    end
  end

  def show
    @photos = @room.photos
  end

  def listing
  end

  def pricing
  end

  def description
  end

  def photo_upload
  end

  def amenities
  end

  def location
  end

  def update
    new_params = room_params
    new_params = room_params.merge(active: true) if is_ready_room

    if @room.update(new_params)
      flash[:notice] = "保存しました。"
    else
      flash[:alert] = "問題が発生しました。"
    end
    redirect_back(fallback_location: request.referer)
  end

  def upload_photo
    @room.photos.attach(params[:file])
    render json: { success: true }
  end

  def delete_photo
    @image = ActiveStorage::Attachment.find(params[:photo_id])
    @image.purge
    redirect_to photo_upload_room_path(@room)
  end

  private

    def set_room
      @room = Room.find(params[:id])
    end
    
    def room_params
      params.require(:room).permit(:home_type, :room_type, :accommodate, :bed_room, :bath_room, :listing_name, :summary, :address, :is_tv, :is_kitchen, :is_air, :is_heating, :is_internet, :price, :active, :description)
    end

    def is_authorised
      redirect_to root_path, alert: "権限がありません。" unless current_user.id == @room.user_id
    end

    def is_ready_room
      !@room.active && !@room.price.blank? && !@room.listing_name.blank? && !@room.photos.blank? && !@room.address.blank?
    end

  end



app\views\rooms\index.html.erb

<div class="row" style="margin: 20px;">
    <!-- 左側 -->
    <div class="col-4">
        <div class="card">
            <div class="card-body">      
                <!-- アバター -->
                <%= image_tag avatar_url(current_user), class: "img-fluid img-thumbnail rounded-pill" %>
                <!-- ユーザー名 -->
                <div class="mx-auto" style="text-align: center;">
                
                    <h5 class="card-title btn-block">
            
                    <br/>
                    <!-- オンラインステータス -->
                    <span style="position:relative; top: 0.8rem; left: 0.5rem;">                        
                    <% if current_user.status %>
                        <span class="badge rounded-pill bg-success text-light">Online</span>
                    <% else %>
                        <span class="badge rounded-pill bg-secondary text-light">Offline</span>
                    <% end %>                                  
                    </span>
                    <%= current_user.full_name %>


                    </h5>
                    <%= current_user.about %>   
                </div>

                <div class="collapse" id="collapseExample3">
                    <%= form_for :user, url: users_edit_url(current_user), action: :update, method: :post do |f| %>
                        <%= f.file_field :avatar, class: "input-group-text", onchange: "this.form.submit();" %>
                    <% end %>   
                </div>
                <br/>
                    <!-- 自己紹介 -->                                                           
                    <div style="margin-left: 20px;">
                                            
                        <div class="collapse" id="collapseExample2">
                            <div class="card card-body" style="width: 16rem;">
                                <%= form_for :user, url: users_edit_url(current_user), action: :update, method: :post do |f| %>
                                    <%= f.text_area :about, autofocus: true, autocomplete: 'form'%>
                                    <br/>
                                    <br/>
                                    <%= f.submit "保存", class: "btn btn-outline-info" %>
                                <% end %>

                            </div>
                        </div>
                        <div class="mx-auto" style="margin-top: 1rem; text-align: right;">
                        <%= l(current_user.created_at, format: :long) %>
                        </div>   
                    </div>

            </div>
        </div>
    </div>
    <!-- 右側 -->
    <div class="col-8" style="margin-left: -4rem;">
 
        <div class="container">
            <div class="row">
                <div class="col-6 col-md-6">
                    <%= link_to new_room_path, style: "text-decoration: none;" do %>
                        <div class="card">
                            <div class="card-body">
                                <%= image_tag 'icon_add.png', style: "width: 50%; display: block; margin: auto;" %>
                                <footer><h5 class="btn btn-outline-info btn-block" style="margin-top: 40px;">部屋新規登録</h5></footer>
                            </div>
                        </div>
                    <% end %>
                </div>
                <!-- 登録したお部屋 -->
                <% current_user.rooms.each do |room| %>
                    <% if room.active? %>
                        <div class="col-6 col-md-6">
                            <div class="card">
                                <div class="card-body">
                                   
                                            <%= image_tag room_cover(room), style: "width: 100%;" %>
                                            <h5 class="card-title"><div class="badge bg-info"><%= link_to room.listing_name, room_path(room), data: { turbolinks: false}, style: "text-decoration: none; color: white;" %></div></h5>
                                            <p style="font-size: 0.8rem; margin-bottom: -0.3rem;">Address</p>
                                            <p style="margin-bottom: 2rem;"><%= room.address %></p>
                                            <!--レビュー -->
                                            <h5 class="badge rounded-pill bg-danger text-light" style="font-size: 1rem;"><%= number_to_currency(room.price) %></h5>
                                            <%= link_to listing_room_path(room), style: "text-decoration: none;" do %>
                                                <h5 class="btn btn-outline-primary btn-block" style="margin-top: 40px; position: relative; bottom: 1.5rem;">登録内容編集</h5>
                                            <% end %>
                                </div>
                            </div>
                        </div>
                    <% end %>
                <% end %>
            </div>
        </div>
    </div>
</div>



app\views\rooms\show.html.erb

<!-- 写真 -->
<div class="row" style="margin-left: 10px; margin-right: 10px;">
    <div class="col-md-11">
        <div class="card" style="width: 66%;">
            <div class="card-body">
                <%= image_tag room_cover(@room), style: "width: 100%" %>
            </div>
        </div>
    </div>
</div>
<br/>
<div class="row" style="margin-left: 10px; margin-right: 10px;">
    <!-- 左パネル -->
    <div class="col-md-8">
        <!-- 部屋の名前 -->
        <div class="row">
            <div class="col-md-11">
                <div class="card">
                    <div class="card-body">
                        <h3 class="badge bg-info" style="font-size: 2rem; color: white;"><%= @room.listing_name %></h3><br/>
                        <label>Address</label>
                        <div style="font-size: 1.5rem; margin-top: -1rem;"><%= @room.address %></div>
                        <div class="text-right">
                            <%= image_tag avatar_url(@room.user), class: "bd-placeholder-img figure-img img-fluid rounded-pill", style: "width: 40px; height: 30px;" %>
                            <%= @room.user.full_name %>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <br/>
        <!-- 部屋のインフォメーション -->
        <div class="row text-babu">
            <div class="col-md-11">
                <div class="card">
                    <div class="card-body">    
                        <div class="row text-center row-space-1">
                            <div class="col-md-3">
                                <i class="fas fa-home fa-2x" style="color: cadetblue"></i><br/>
                                <%= @room.home_type %>
                            </div>
                            <div class="col-md-3">
                                <i class="fas fa-user fa-2x" style="color: cadetblue"></i><br/>
                                <%= pluralize(@room.accommodate, "人宿泊可能") %>
                            </div>
                            <div class="col-md-3">
                                <i class="fas fa-bed fa-2x" style="color: cadetblue"></i><br/>
                                <%= pluralize(@room.bed_room, "台") %>
                            </div>
                            <div class="col-md-3">
                                <i class="fas fa-door-closed fa-2x" style="color: cadetblue"></i><br/>
                                <%= pluralize(@room.bath_room, "部屋") %>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <br/>
        <!-- 詳細 -->
        <div class="row">
            <div class="col-md-11">
                <div class="card">
                    <div class="card-body">        
                        <h4 class="badge bg-primary" style="font-size: 1.5rem; color: white;">部屋の詳細</h4>
                        <p><font style="font-size: 1.2rem;"><%= @room.description %></font></p>
                    </div>
                </div>
            </div>
        </div>
        <br/>

        <!-- アメニティ -->
        <div class="row">
            <div class="col-md-11">
                <div class="card">
                    <div class="card-body">        
                        <h4 class="badge bg-warning" style="font-size: 1.5rem; color: white;">アメニティ</h4>

                        <div class="col-md-8">
                        <div class="row">
                            <div class="col-md-5">
                                <ul class="amenities">
                                    <li class="<%= 'text-line-through' if !@room.is_tv %>"><span style="color: black;"><i class="fas fa-tv"></i>&nbsp;テレビ</span></li>
                                    <li class="<%= 'text-line-through' if !@room.is_kitchen %>"><span style="color: black;"><i class="fas fa-blender"></i>&nbsp;キッチン</span></li>
                                    <li class="<%= 'text-line-through' if !@room.is_internet %>"><span style="color: black;"><i class="fas fa-wifi"></i>&nbsp;インターネット</span></li>
                                </ul>
                            </div>
                            <div class="col-md-5">
                                <ul class="amenities">
                                    <li class="<%= 'text-line-through' if !@room.is_heating %>"><span style="color: black;"><i class="fab fa-hotjar"></i>&nbsp;暖房</span></li>
                                    <li class="<%= 'text-line-through' if !@room.is_air %>"><span style="color: black;"><i class="fas fa-temperature-low"></i>&nbsp;エアコン</span></li>
                                </ul>
                            </div>
                        </div>
                    </div>                        



                    </div>
                </div>
            </div>
        </div>
    
        <br/>
        <!-- レビュー -->
        <br/>
        <!-- カルーセル表示 -->
        <div class="row">
            <div class="col-md-11">
                <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
                <% if @photos.length > 0 %>    
                    <ol class="carousel-indicators">
                        <% @photos.each do |photo| %>
                            <li data-target="#carouselExampleIndicators" data-slide-to="<%= photo.id %>" class="<%= 'active' if photo.id == @photos[0].id %>"></li>
                        <% end %>
                    </ol>
                    <div class="carousel-inner">
                        <% @photos.each do |photo| %>
                            <div class="carousel-item <%= 'active' if photo.id == @photos[0].id %>">
                            <%= image_tag url_for(photo), class: "bd-placeholder-img bd-placeholder-img-lg d-block w-100", width: "100%" %>
                            </div>
                        <% end %>
                        <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
                        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                        <span class="sr-only">Previous</span>
                        </a>
                        <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
                        <span class="carousel-control-next-icon" aria-hidden="true"></span>
                        <span class="sr-only">Next</span>
                        </a>
                <% end %>
                    </div>
            </div>
        </div>
        <hr/>
        <!-- googleマップ -->
        <!-- 近くのお部屋を検索 -->
    </div>
</div>
<!-- 右パネル -->
<div class="col-md-4">
    <!-- 予約フォーム -->
    
</div>



ブラウザ確認
表示されるか確認して下さい。
http://localhost:3000/rooms
http://localhost:3000/rooms/1

部屋管理
部屋管理


部屋詳細
部屋詳細



ナビゲーションバーにリンクを追加します。


app\views\shared\_navbar.html.erb

<nav class="navbar navbar-expand-lg navbar-dark bg-dark" style="z-index: 5;">
  <a class="navbar-brand" href="/"><h1 class="navh1"><font style="font-size: 1.2rem"><b>民泊予約サイト</font> <font style="font-size: 0.9rem">Private lodging reservation site</b></font></h1></a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse justify-content-end" id="navbarNavDropdown">
    <ul class="navbar-nav">
    <!-- もしログインしていなかったら-->
    <% if (!user_signed_in?) %>
      <li class="nav-item" style="margin-right: 20px; margin-bottom: 5px;">
        <%= link_to  "まずはユーザ登録 Sign up", new_user_registration_path, class: "btn btn-danger btn-sm" %>
      </li>
      <li class="nav-item">
        <%= link_to  "ユーザ登録がお済みの方はこちら login", new_user_session_path, class: "btn btn-info btn-sm", style: "margin-right: 80px;" %>
      </li>
    <!-- ログインしていたら -->
    <% else %>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">

        <figure style="position:relative; top: 0.2rem;" class="avatar <%= current_user.status ? "online" : "offline" %>"></figure>  
        <%= image_tag avatar_url(current_user), class: "bd-placeholder-img figure-img img-fluid rounded-pill", style: "width: 40px; height: 30px; margin-top: 0.5rem;" %>

        <div class="btn btn-light bg-light text-dark"><%= current_user.full_name %></div>
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
        <span class="dropdown-item"><i class="fas fa-user-edit"></i>&nbsp;<%= link_to  "ユーザ登録情報編集", edit_user_registration_path, class: "btn btn-warning" %></span>
          <span class="dropdown-item"><i class="fas fa-sign-out-alt"></i>&nbsp;<%= link_to  "ログアウト", destroy_user_session_path, method: :delete, class: "btn btn-danger" %></span>
        </div>
      </li>
    <% end %>
    </ul>
  </div>
</nav>
<% if (user_signed_in?) %>
  <nav class="navbar navbar-expand-lg navbar-light bg-info" style="z-index: 3;">
    <div class="collapse navbar-collapse justify-content-end" id="navbarNavDropdown">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item" style="margin-top: 7px;">
        <i class="fas fa-tachometer-alt" style="color: white;"></i>&nbsp;<%= link_to 'ダッシュボード Dashboard', dashboard_path, class: "btn btn-light" %>
        </li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          <i class="fas fa-hospital-symbol" style="color: white;"></i>&nbsp;<div class="btn btn-light bg-light text-dark">ホスト Host</div>
          </a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <span class="dropdown-item"><i class="fas fa-edit"></i></i><%= link_to  "部屋を新規登録", new_room_path, class: "btn btn-primary", style: "margin-left: 0.5rem;" %></span>
            <span class="dropdown-item"><i class="far fa-list-alt"></i><%= link_to  "部屋管理", rooms_path, class: "btn btn-success", style: "margin-left: 0.5rem;" %></span>
          </div>
        </li>&nbsp;&nbsp;
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          <i class="fas fa-user-friends" style="color: white;"></i>&nbsp;<div class="btn btn-light bg-light text-dark">ゲスト Guest</div>
          </a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          </div>
        </li>
      </ul>
    </div>
  </nav>
<% end %>



↓↓クリックして頂けると励みになります。


| 23 | アクションテキスト <<  [ホーム] >> | 25 | Googleマップ