[19]部屋モデル作成<< [ホームに戻る] >> [21]部屋ビュー作成
コマンド
1文です。
rails g controller Rooms index new create listing pricing description photo_upload amenities location update
app\controllers\rooms_controller.rb
class RoomsController < ApplicationController before_action :set_room, except: [:index, :new, :create] before_action :authenticate_user!, except: [:show] 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 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 if @room.update(new_params) flash[:notice] = "保存しました。" else flash[:alert] = "問題が発生しました。" end redirect_back(fallback_location: request.referer) 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) end end
「app\views\rooms」フォルダに「show.html.erb」ファイルを新規作成します。
今は空のファイルで大丈夫です。
記述追加 config\routes.rb(17行目)
resources :rooms, except: [:edit] do member do get 'listing' get 'pricing' get 'description' get 'photo_upload' get 'amenities' get 'location' end end
config\routes.rb
自動で追加された記述は削除します。
Rails.application.routes.draw do # ルートを app\views\pages\home.html.erb に設定 root 'pages#home' devise_for :users, path: '', path_names: {sign_up: 'register', sign_in: 'login', edit: 'profile', sign_out: 'logout'}, controllers: {registrations: 'registrations'} get 'pages/home' get '/dashboard', to: 'users#dashboard' get '/users/:id', to: 'users#show', as: 'user' post '/users/edit', to: 'users#update' resources :rooms, except: [:edit] do member do get 'listing' get 'pricing' get 'description' get 'photo_upload' get 'amenities' get 'location' end end # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end
↓↓クリックして頂けると励みになります。