実現したいこと
宿泊予約アプリを作成中です。
その中で施設の登録機能を実装しようとしています。
発生している問題・分からないこと
コードをある程度記載したので、動作確認をしようとした所別のviewファイルが表示されます。
エラーコードは特に表示されませんが、ターミナルに下記の表示がありました。
エラーメッセージ
error
1Started GET "/rooms/new" for ::1 at 2024-05-04 01:18:22 +0900 2Processing by RoomsController#new as HTML 3Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms | Allocations: 251) 4 5Started GET "/users/sign_in" for ::1 at 2024-05-04 01:18:22 +0900 6Processing by Users::SessionsController#new as HTML 7
該当のソースコード
rooms_controller.rb
1class RoomsController < ApplicationController 2 3 before_action :set_room, except: [:index, :new, :create] 4 before_action :authenticate_user!, except: [:show] 5 6 def index 7 @rooms = Room.all 8 end 9 10 def new 11 @room = current_user.rooms.build 12 end 13 14 def create 15 @room = current_user.rooms.build(room_params) 16 if @room.save 17 redirect_to listing_room_path(@room), notice: "保存しました。" 18 else 19 flash[:alert] = "問題が発生しました。" 20 render :new 21 end 22 end 23 def show 24 end 25 def listing 26 end 27 def pricing 28 end 29 def description 30 end 31 def photo_upload 32 end 33 def amenities 34 end 35 def location 36 end 37 def update 38 new_params = room_params 39 if @room.update(new_params) 40 flash[:notice] = "保存しました。" 41 else 42 flash[:alert] = "問題が発生しました。" 43 end 44 redirect_back(fallback_location: request.referer) 45 end 46 private 47 def set_room 48 @room = Room.find(params[:id]) 49 end 50 def room_params 51 params.require(:room).permit(:r_name, :r_icon, :r_self, :r_cost, :r_create, r_address) 52 end 53end
routes.rb
1Rails.application.routes.draw do 2 devise_for :users, controllers: { 3 registrations: 'users/registrations', 4 sessions: 'users/sessions' 5 } 6 devise_scope :user do 7 get '/users/sign_in', to: 'users/sessions#new' 8 get '/users/sign_out', to: 'users/sessions#destroy' 9 end 10 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 11 resources :formats, only: [:index, :new, :create, :show] 12 13 # トップページ 14 root 'formats#index' 15 get 'formats/index' 16 #ユーザーページ確認 17 get "users/show" 18 #新規ユーザーページ 19 get "signup" => "users#new" 20 post "users/create" => "users#create" 21 #ユーザーページの編集 22 get "users/:id/edit" => "users#edit" 23 get "signup" => "users#new" 24 post "users/create" => "users#create" 25 26 #部屋 27 resource :rooms, except: [:edit] do 28 get "rooms/new" => "rooms#new" 29 30 member do 31 get 'listing' 32 get 'photo_upload' 33 end 34 end 35end
new.html.erb
1<div class="container"> 2 <div class="card mt-4"> 3 <div class="card-body"> 4 <h4 class="mt-4 mb-4"><b>部屋の新規登録</b></h4> 5 <%= form_for @room do |f| %> 6 <div class="col-md-4"> 7 <div class="mb-2"> 8 <label>お部屋のタイプ</label> 9 <%= f.select :room_type, [ ["シングル", "シングル"], ["ツイン", "ツイン"]], 10 id: "room_type", prompt: "選択してください", class: "form-control" %> 11 </div> 12 </div> 13 <div class="col-md-4"> 14 <div class="mb-2"> 15 <label>宿泊可能人数</label> 16 <%= f.select :accommodate, [["2人", 2], ["3人", 3], ["4人", 4], ["5人", 5], ["6人", 6]], 17 id: "accommodate", prompt: "選択してください", class: "form-control" %> 18 </div> 19 </div> 20 </div> 21 <div class="col-md-4"> 22 <div class="mb-2"> 23 <label>部屋数</label> 24 <%= f.select :bath_room, [["1部屋", 1], ["2部屋", 2], ["3部屋", 3], ["4部屋", 4], ["5部屋", 5], ["6部屋", 6]], 25 id: "bath_rooms", prompt: "選択してください", class: "form-control" %> 26 </div> 27 </div> 28 <div class="col-md-4"> 29 </div> 30 </div> 31 <%= f.submit "登録する", class: "btn btn-danger w-100" %> 32 <% end %> 33 </div> 34 </div> 35</div>
application_controller.rb
1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 before_action :configure_permitted_parameters, if: :devise_controller? 4 5 protected 6 7 def configure_permitted_parameters 8 added_attrs = [ :name, :mail, :pass ] 9 devise_parameter_sanitizer.permit :sign_up, keys: added_attrs 10 devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :mail]) 11 devise_parameter_sanitizer.permit(:account_update, keys: [:name, :self]) 12 end 13 def after_sign_in_path_for(resource) 14 users_show_path 15 end 16end
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
作成にあたり参考にしていたサイトを確認しましたが、抜けなどは見当たりませんでした。
エラーコードに記載した文を検索し、deviseの認証が問題という記載を見つけましたが、そもそもログインしていないのでそれは該当しないかと思います。
補足
特になし
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。