showアクションにて、URLに表示されているidを
@timetable = User.find_by_id(params[:id])でurlのidを取得しつつ、viewのほうには
Timetableテーブルの値を表示したいです。
viewを表示するにはログインユーザーのidを渡してあげないとroot_pathに飛ばされるようになっています。
うまいやり方が思いつきません。よろしくお願いいたします。
DB
1timetablesテーブルにあるカラム 2id, user_id, user_name, date, hour, minute 3 4 5Usersテーブルにあるカラム 6id, name 7
TimetableController
1class TimetablesController < ApplicationController 2 before_action :logged_in_timetable, only: [:edit, :update, :show] 3 before_action :correct_timetable, only: [:edit, :update, :show] 4 before_action :set_timetable, only: [:destroy] 5 6 def timetable_index 7 @timetables = Timetable.all 8 9 @times0900 = Timetable.where(hour: "9", minute: "00") 10 @times0930 = Timetable.where(hour: "9", minute: "30") 11 @times1000 = Timetable.where(hour: "10", minute: "00") 12 @times1030 = Timetable.where(hour: "10", minute: "30") 13 @times1100 = Timetable.where(hour: "11", minute: "00") 14 @times1130 = Timetable.where(hour: "11", minute: "30") 15 @times1200 = Timetable.where(hour: "12", minute: "00") 16 @times1230 = Timetable.where(hour: "12", minute: "30") 17 @times1500 = Timetable.where(hour: "15", minute: "00") 18 @times1530 = Timetable.where(hour: "15", minute: "30") 19 @times1600 = Timetable.where(hour: "16", minute: "00") 20 @times1630 = Timetable.where(hour: "16", minute: "30") 21 @times1700 = Timetable.where(hour: "17", minute: "00") 22 @times1730 = Timetable.where(hour: "17", minute: "30") 23 @times1800 = Timetable.where(hour: "18", minute: "00") 24 @times1830 = Timetable.where(hour: "18", minute: "30") 25 @times1900 = Timetable.where(hour: "19", minute: "00") 26 @times1930 = Timetable.where(hour: "19", minute: "30") 27 end 28 29 def new 30 @timetable = Timetable.new 31 end 32 33 def create 34 @timetable = Timetable.new(user_id: current_user.id, user_name: current_user.name, date: timetable_params[:date], hour: timetable_params[:hour], minute: timetable_params[:minute]) 35 if @timetable.save 36 flash[:success] = "ご予約ありがとうございます" 37 redirect_to root_path 38 else 39 flash[:success] = "申し訳ございません。予約を受け付けられませんでした" 40 redirect_to root_path 41 end 42 end 43 44 def show 45 @timetable = User.find_by_id(params[:id]) 46 end 47//本当はshowアクションの中を 48//@timetable = Timetable.find_by_id(params[:id])としたいのですが、(params[:id])ではUrlに表示されているidを取得するため、Userテーブルのidとは一致しません。 49 50//もし、@timetable = Timetable.find_by_id(params[:id])みたいな感じでTimetabeslテーブルからidカラムではなくuser_idカラムをとることができればこの問題は解決すると思うのですが。。。 51 52// viewにtimetablesのuser_name, date, hour, minuteを表示したいです。 53 54 55 def edit 56 @timetable = Timetable.find(params[:id]) 57 58 @times0900 = Timetable.where(hour: "9", minute: "00") 59 @times0930 = Timetable.where(hour: "9", minute: "30") 60 @times1000 = Timetable.where(hour: "10", minute: "00") 61 @times1030 = Timetable.where(hour: "10", minute: "30") 62 @times1100 = Timetable.where(hour: "11", minute: "00") 63 @times1130 = Timetable.where(hour: "11", minute: "30") 64 @times1200 = Timetable.where(hour: "12", minute: "00") 65 @times1230 = Timetable.where(hour: "12", minute: "30") 66 @times1500 = Timetable.where(hour: "15", minute: "00") 67 @times1530 = Timetable.where(hour: "15", minute: "30") 68 @times1600 = Timetable.where(hour: "16", minute: "00") 69 @times1630 = Timetable.where(hour: "16", minute: "30") 70 @times1700 = Timetable.where(hour: "17", minute: "00") 71 @times1730 = Timetable.where(hour: "17", minute: "30") 72 @times1800 = Timetable.where(hour: "18", minute: "00") 73 @times1830 = Timetable.where(hour: "18", minute: "30") 74 @times1900 = Timetable.where(hour: "19", minute: "00") 75 @times1930 = Timetable.where(hour: "19", minute: "30") 76 end 77 78 def update 79 @timetable = Timetable.find(params[:id]) 80 if @timetable.update_attributes(timetable_params) 81 flash[:success] = "予約時間を変更しました。" 82 redirect_to timetable_path(current_user) 83 else 84 flash[:danger] = "予約を受け付けられませんでした。" 85 render 'edit' 86 87 end 88 end 89 90 def destroy 91 @timetable.destroy 92 flash[:success] = "予約が取り消されました。" 93 redirect_to root_path 94 end 95 96 private 97 def timetable_params 98 params.require(:timetable).permit(:user_id, :user_name, :date, :hour, :minute) 99 end 100 101 def logged_in_timetable 102 unless logged_in? 103 flash[:danger] = "Please log in." 104 redirect_to login_url 105 end 106 end 107 108 def correct_timetable 109 @timetable = User.find(params[:id]) 110 redirect_to(root_url) unless @timetable == current_user 111 112 end 113 114 def set_timetable 115 @timetable = Timetable.find(params[:id]) 116 end 117end 118
view
1<% if @timetable.nil? %> 2 <h1>予約が存在しません</h1> 3 <% else %> 4 <h1><%= @timetable.user_name %>さんの次の予約は<h1> 5 <h3><%= @timetable.date %>日の<%= @timetable.hour %>時<%= @timetable.minute %>時からです</h3> 6 <%= link_to "予約時間を変更する", edit_timetable_path(current_user) %> 7 <%= link_to "予約を取り消す", @timetable, method: :delete, data: { confirm: "予約を消去します。よろしですか?"} %> 8<% end %>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/18 07:20
2020/02/18 08:39
2020/02/18 11:31
2020/02/18 11:32
2020/02/18 11:39
2020/02/18 11:45
2020/02/18 12:33