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

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

Rails6.0 | 民泊サイトの構築(改良版)| 20 | 部屋ビューの作成 | for MacOSX



| 19 | 部屋コントローラーの作成 <<  [ホーム] >> | 21 | 複数の写真をアップロード


app\views\room\new.html.erb

<br/>
<br/>
<div class="row">
    <div class="col-sm-9" style="margin:0 auto;">
        <div class="card text-center">
            <h4 class="card-header text-center text-light bg-primary">部屋の新規登録</h4>
                <div class="card-body" style="margin-left: 50px;">
                <br/>
                <%= form_for @room do |f| %>
                    <div class="row">
                        <div class="col-md-4 select">
                            <div class="form-group">
                                <label>お家のタイプ</label>
                                <%= f.select :home_type, [["マンション", "マンション"], ["アパート", "アパート"], ["一戸建て", "一戸建て"]],
                                    id: "home_type", prompt: "選択してください", class: "form-control" %>
                            </div>
                        </div>
                        <br/>
                        <div class="col-md-4 select">
                            <div class="form-group">
                                <label>お部屋のタイプ</label>
                                <%= f.select :room_type, [ ["プライベート", "プライベート"], ["シェア", "シェア"]],
                                    id: "room_type", prompt: "選択してください", class: "form-control" %>
                            </div>
                        </div>
                        <br/>
                        <div class="col-md-4 select">
                            <div class="form-group">
                                <label>宿泊可能人数</label>
                                <%= f.select :accommodate, [["2人", 2], ["3人", 3], ["4人", 4], ["5人", 5], ["6人", 6]],
                                    id: "accommodate", prompt: "選択してください", class: "form-control" %>
                            </div>
                        </div>
                        <br/>
                        <div class="col-md-4 select">
                            <div class="form-group">
                                <label>ベッド数</label>
                                <%= f.select :bed_room, [["1台", 1], ["2台", 2], ["3台", 3], ["4台", 4], ["5台", 5], ["6台", 6]],
                                    id: "bed_room", prompt: "選択してください", class: "form-control" %>
                            </div>
                        </div>
                        <br/>
                        <div class="col-md-4 select">
                            <div class="form-group">
                                <label>部屋数</label>
                                <%= f.select :bath_room, [["1部屋", 1], ["2部屋", 2], ["3部屋", 3], ["4部屋", 4], ["5部屋", 5], ["6部屋", 6]],
                                    id: "bath_rooms", prompt: "選択してください", class: "form-control" %>
                            </div>
                        </div>
                    </div>
                    <br/>
          
                    <br/>
                    <%= f.submit "登録する", class: "btn btn-outline-primary btn-block" %>
                    <% end %>
                </div>
        </div>      
    </div>
</div>



サーバ起動
rails s


ブラウザ確認
http://localhost:3000/rooms/new

部屋の新規登録
部屋の新規登録



「app\views\rooms」フォルダに「_room_menu.html.erb」ファイルを新規作成


app\views\rooms\_room_menu.html.erb(新規作成したファイル)

<br/>
<div class="row" style="margin-left: 50px;">
        <div class="card text-center">
                <div class="card-body">
                <br/>
                <ul class="sidebar-list" style="margin-left: 30px; margin-right: 30px;">
                <li class="sidebar-item">
                  <%= link_to "お部屋の概要", listing_room_path, class: "sidebar-link active" %>
                  <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
                </li>
                <li class="sidebar-item">
                  <%= link_to "1泊の宿泊価格", pricing_room_path, class: "sidebar-link active" %>
                  <% if !@room.price.blank? %>
                    <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
                  <% end %>
                </li>
                <li class="sidebar-item">
                  <%= link_to "お部屋の名前と説明", description_room_path, class: "sidebar-link active" %>
                  <% if !@room.listing_name.blank? %>
                    <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
                  <% end %>
                </li>
                <li class="sidebar-item">
 
                </li>
                <li class="sidebar-item">
                  <%= link_to "付属設備", amenities_room_path, class: "sidebar-link active" %>
                  <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
                </li>
                <li class="sidebar-item">
                  <%= link_to "お部屋の住所", location_room_path, class: "sidebar-link active" %>
                  <% if !@room.address.blank? %>
                    <span class="pull-right text-babu"><i class="fa fa-check"></i></span>
                  <% end %>
                </li>
              </ul>
              <hr/>
              <% is_ready = !@room.active && !@room.price.blank? && !@room.listing_name.blank? && !@room.address.blank? %>
              <%= form_for @room do |f| %>
                <%= f.hidden_field :active, value: true %>
                <%= f.submit "公開する", id: "publish_button", class: "btn btn-danger btn-block", disabled: !is_ready %>
              <% end %>
                </div>
        </div>      
</div>



app\views\rooms\photo_upload.html.erb

<br/>
<div class="row">
    <div class="col-md-3">
        <%= render 'room_menu' %>
    </div>
    <div class="col-md-9">
        <div class="row">
            <div class="col-sm-9">
                <br/>
                <div class="card text-center">
                    <h4 class="card-header text-center">お部屋の写真 登録</h4>
                    <div class="card-body" style="margin-left: 50px;">
                        <br/>
                        <!--写真アップロードの記述 -->
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



app\views\rooms\listing.html.erb

<br/>
<div class="row">
    <div class="col-md-3">
        <%= render 'room_menu' %>
    </div>
    <div class="col-md-9">
        <div class="row">
            <div class="col-sm-10">
                <br/>
                <div class="card text-center">
                    <h4 class="card-header text-center text-light bg-secondary">登録情報の修正</h4>
                    <div class="card-body" style="margin-left: 50px;">
                        <br/>
                        <%= form_for @room do |f| %>
                            <div class="row">
                                <div class="col-md-4 select">
                                    <div class="form-group">
                                        <label>お家のタイプ</label>
                                        <%= f.select :home_type, [["マンション", "マンション"], ["アパート", "アパート"], ["一戸建て", "一戸建て"]],
                                            id: "home_type", prompt: "選択してください", class: "form-control" %>
                                    </div>
                                </div>
                                <br/>
                                <div class="col-md-4 select">
                                    <div class="form-group">
                                        <label>お部屋のタイプ</label>
                                        <%= f.select :room_type, [ ["プライベート", "プライベート"], ["シェア", "シェア"]],
                                            id: "room_type", prompt: "選択してください", class: "form-control" %>
                                    </div>
                                </div>
                                <br/>
                                <div class="col-md-4 select">
                                    <div class="form-group">
                                        <label>宿泊可能人数</label>
                                        <%= f.select :accommodate, [["2人", 2], ["3人", 3], ["4人", 4], ["5人", 5], ["6人", 6]],
                                            id: "accommodate", prompt: "選択してください", class: "form-control" %>
                                    </div>
                                </div>
                                <br/>
                                <div class="col-md-4 select">
                                    <div class="form-group">
                                        <label>ベッド数</label>
                                        <%= f.select :bed_room, [["1台", 1], ["2台", 2], ["3台", 3], ["4台", 4], ["5台", 5], ["6台", 6]],
                                            id: "bed_room", prompt: "選択してください", class: "form-control" %>
                                    </div>
                                </div>
                                <br/>
                                <div class="col-md-4 select">
                                    <div class="form-group">
                                        <label>部屋数</label>
                                        <%= f.select :bath_room, [["1部屋", 1], ["2部屋", 2], ["3部屋", 3], ["4部屋", 4], ["5部屋", 5], ["6部屋", 6]],
                                            id: "bath_rooms", prompt: "選択してください", class: "form-control" %>
                                    </div>
                                </div>
                            </div>

                            <br/>
                            <div><%= f.submit "修正する", class: "btn btn-outline-secondary btn-block" %></div>
                        <% end %>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



app\views\rooms\amenities.html.erb

<br/>
<div class="row">
    <div class="col-md-3">
        <%= render 'room_menu' %>
    </div>
    <div class="col-md-9">
        <div class="row">
            <div class="col-sm-9">
                <br/>
                <div class="card text-center">
                    <h4 class="card-header text-center text-light bg-secondary">附属設備</h4>
                    <div class="card-body" style="margin-left: 50px;">
                        <br/>
                        <%= form_for @room do |f| %>
                            <div class="row form-group">
                               <div class="col-md-4">
                                 <%= f.check_box :is_tv %> テレビ
                               </div>
                               <div class="col-md-4">
                                 <%= f.check_box :is_kitchen %> キッチン
                               </div>
                               <div class="col-md-4">
                                 <%= f.check_box :is_internet %> インターネット
                               </div>
                             </div>
                             <div class="row form-group">
                               <div class="col-md-4" style="margin-left: -0.5rem;">
                                 <%= f.check_box :is_heating %> 暖房
                               </div>
                               <div class="col-md-4" style="margin-left: 0.5rem;">
                                 <%= f.check_box :is_air %> エアコン
                               </div>
                             </div>
                             <br/><br/>
                             <div class="text-center">
                               <%= f.submit "保存", class: "btn btn-outline-secondary btn-block" %>
                             </div>
                            <% end %>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



app\views\rooms\description.html.erb

<br/>
<div class="row">
    <div class="col-md-3">
        <%= render 'room_menu' %>
    </div>
    <div class="col-md-9">
        <div class="row">
            <div class="col-sm-9">
                <br/>
                <div class="card text-center">
                    <h4 class="card-header text-center text-light bg-secondary">部屋の名前と説明</h4>
                    <div class="card-body">
                        <br/>
                        <%= form_for @room do |f| %>
                            <div class="row" style="margin-left: 40px;">
                                <label>部屋の名前</label>
                                <%= f.text_field :listing_name, placeholder: "部屋の名前", class: "form-control", required: true, id: "autoaddress", style: "width: 100%;" %>
                           </div>
                           <br/>
                           <div class="row" style="margin-left: 40px;">

                            <label>説明</label>
                            <%= f.text_area :summary, style: "text-align: left; width: 100%; height: 8rem;" %>

                          </div>
                          <br/>
                           <div class="text-center">
                            <%= f.submit "保存", class: "btn btn-outline-secondary btn-block" %>
                           </div>
                        <% end %>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



app\views\rooms\location.html.erb

<br/>
<div class="row">
    <div class="col-md-3">
        <%= render 'room_menu' %>
    </div>
    <div class="col-md-9">
        <div class="row">
            <div class="col-sm-9">
                <br/>
                <div class="card text-center">
                    <h4 class="card-header text-center text-light bg-secondary">部屋の住所</h4>
                    <div class="card-body" style="margin-left: 50px;">
                        <br/>
                        <%= form_for @room do |f| %>
                            <div class="row">

                                <%= f.label :住所 %>
                                <%= f.text_field :address, placeholder: "住所を入力", class: "form-control", required: true, id: "autoaddress", style: "width: 100%;" %>
                              </div>

                           <br/>
                           <div class="text-center">
                            <%= f.submit "保存", class: "btn btn-outline-secondary btn-block" %>
                           </div>
                        <% end %>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



app\views\rooms\pricing.html.erb

<br/>
<div class="row">
    <div class="col-md-3">
        <%= render 'room_menu' %>
    </div>
    <div class="col-md-9">
        <div class="row">
            <div class="col-sm-10">
                <br/>
                <div class="card text-center">
                    <h4 class="card-header text-center text-light bg-secondary">1泊の価格</h4>
                    <div class="card-body" style="margin-left: 50px;">
                        <br/>
                        <%= form_for @room do |f| %>
                            <div class="">
                                <div class="form-group">
                                    <div style="text-align: right;">価格(円)</div>
                                    <%= f.text_field :price, placeholder: "例: 5,000",class: "form-control", required: true, id: "autoaddress" %>
                                </div>
                        
                            </div>
                            <br/>
                            <br/>

                                <%= f.submit "保存", class: "btn btn-outline-secondary btn-block" %>


                        <% end %>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



ブラウザ確認
http://localhost:3000/rooms/new

部屋が登録できるか確認してみましょう。


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


| 19 | 部屋コントローラーの作成 <<  [ホーム] >> | 21 | 複数の写真をアップロード

YAE C5 CLINIC(札幌美容クリニック)

関連記事(外部サイト)