###前提・実現したいこと
ruby on railsで予約機能を作っているのですが、controllerのエラーが発生しました。
ルーティング、コントローラー、ビューを確かめたのですが、原因がわからず質問させていただきます。
初心者で、初歩的なミスかもしれませんが、ご指摘お願いいたします。
###発生している問題・エラーメッセージ
ActionController::ActionControllerError in ReservationsController#create Cannot redirect to nil! Extracted source (around line #5): 3 @reservation = current_user.reservations.create(reservation_params) 4 5 redirect_to @reservation.listing, notice:"予約が完了しました" 6 end 7 8 private Rails.root: /Users/komorigasa/Desktop/Travelshare2 Application Trace | Framework Trace | Full Trace app/controllers/reservations_controller.rb:5:in `create' Request Parameters: {"utf8"=>"✓", "authenticity_token"=>"i95b+wiL2o0qhDOP8tI24bkMOSL6EsE/k2BfKRQSZHtBhxKip22jJ5Q3DemUO/rUNz6T9F+wnPAXnfh9LU6kMA==", "reservation"=>{"listing_id"=>"4", "price"=>"88", "total_price"=>"", "by_time"=>"09/12"}, "commit"=>"予約する", "listing_id"=>"4"}
###route.rb
Rails.application.routes.draw do resources :listings root :to => "pages#index" devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks', registrations: 'registrations' } resources :users, only: [:show] resources :photos, only: [:create, :destroy] do collection do get :list end end resources :listings do resources :reservations, only: [:create] end get 'manage-listing/:id/basics' => 'listings#basics', as: 'manage_listing_basics' get 'manage-listing/:id/description' => 'listings#description', as: 'manage_listing_description' get 'manage-listing/:id/address' => 'listings#address', as: 'manage_listing_address' get 'manage-listing/:id/price' => 'listings#price', as: 'manage_listing_price' get 'manage-listing/:id/photos' => 'listings#photos', as: 'manage_listing_photos' get 'manage-listing/:id/calendar' => 'listings#calendar', as: 'manage_listing_calendar' get 'manage-listing/:id/bankaccount' => 'listings#bankaccount', as: 'manage_listing_bankaccount' get 'manage-listing/:id/publish' => 'listings#publish', as: 'manage_listing_publish' end
###reservations_controller.rb
class ReservationsController < ApplicationController def create @reservation = current_user.reservations.create(reservation_params) redirect_to @reservation.listing, notice:"予約が完了しました" end private def reservation_params params.require(:reservation).permit(:number, :by_time, :price, :total_price, :commodity_price) end end
###show.html.erb (追記)
<!-- 予約フォーム --> <div class="col-md-3"> <div class="panel panel-default panel-show"> <div class="panel-heading"> <span style="font-size:16px;"><%= @listing.price %>円(1荷物(=1Kgまで)の料金)</span> </div> <div class="panel-body panel-real"> <%= form_for [@listing, @listing.reservations.new] do |f| %> <!-- 予約時に一緒におくるデータ--> <%= f.hidden_field :listing_id, value: @listing.id %> <%= f.hidden_field :price, value: @listing.price %> <%= f.hidden_field :total_price, id:'reservation_total_price' %> <div class="row row-space-2"> <div class="col-md-6"> <label>欲しい商品の価格</label> <div class="input-group"> <%= f.text_field :commodity_prices, :class => 'form-control', placeholder: '例: 1000' %> <span class="input-group-addon" id="basic-addon2">円</span> </div> </div> <div class="col-md-6"> <label>期限</label> <%= f.text_field :by_time, :class => 'form-control', placeholder: '期限' %> </div> </div> <div class="actions text-center"> <%= f.submit "予約する", class: "btn btn-danger btn-wide" %> </div> <% end %> </div> </div> </div>
###path
Prefix Verb URI Pattern Controller#Action listings GET /listings(.:format) listings#index POST /listings(.:format) listings#create new_listing GET /listings/new(.:format) listings#new edit_listing GET /listings/:id/edit(.:format) listings#edit listing GET /listings/:id(.:format) listings#show PATCH /listings/:id(.:format) listings#update PUT /listings/:id(.:format) listings#update DELETE /listings/:id(.:format) listings#destroy root GET / pages#index new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) users/omniauth_callbacks#passthru user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) users/omniauth_callbacks#facebook new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) registrations#cancel new_user_registration GET /users/sign_up(.:format) registrations#new edit_user_registration GET /users/edit(.:format) registrations#edit user_registration PATCH /users(.:format) registrations#update PUT /users(.:format) registrations#update DELETE /users(.:format) registrations#destroy POST /users(.:format) registrations#create user GET /users/:id(.:format) users#show listing_reservations POST /listings/:listing_id/reservations(.:format) reservations#create GET /listings(.:format) listings#index POST /listings(.:format) listings#create GET /listings/new(.:format) listings#new GET /listings/:id/edit(.:format) listings#edit GET /listings/:id(.:format) listings#show PATCH /listings/:id(.:format) listings#update PUT /listings/:id(.:format) listings#update DELETE /listings/:id(.:format) listings#destroy manage_listing_basics GET /manage-listing/:id/basics(.:format) listings#basics
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/10 06:35 編集
2018/01/10 06:40
2018/01/10 06:40
2018/01/10 06:44
2018/01/10 06:49
2018/01/10 06:57
2018/01/10 07:14
2018/01/10 09:59