前提
保育園や幼稚園の「連絡帳」をWeb化できるよう、Railsで「連絡帳アプリ」を作成中です。
連絡帳のやり取りは、chatアプリを参考に作成していましたが、行き詰まってしまったので、質問させていただきます。
実装の考え方などアドバイスいただけましたら幸いです。
実現したいこと
①管理者(保育者)ユーザー(保護者)どちらも連絡帳を投稿することができる。
②管理者からは、どのユーザーの連絡帳なのか選択することができる
イメージとしては投稿画面の前に、ユーザー選択一覧ページがある
③ユーザーからは管理者のみを選ぶことができる、または②のような選択画面はなく直接投稿ページに移動する
現在はユーザーが投稿するときも②のページが表示されるため、他のユーザーの連絡帳を投稿できるようになってしまっています。
controllerやviewで「admin」を上手く利用して制御することは出来るのでしょうか?
補足
・ログイン機能:devise ・管理者:Userテーブルにboolean型adminカラムを設定 ・Chatアプリを参考に中間テーブルを作成、各ユーザー同士の部屋ができ、 その中で連絡帳のやり取りができるように考えた。
該当のソースコード
app/models/user.rb
1 has_many :posts, dependent: :destroy 2 has_many :user_boxes, dependent: :destroy 3 has_many :boxes, through: :user_boxes
app/models/post.rb
1 belongs_to :user 2 belongs_to :box 3 validates :user_id, :box_id, presence: true
app/models/box.rb
1 has_many :posts, dependent: :destroy 2 has_many :user_boxes, dependent: :destroy 3 has_many :users, through: :user_boxes
app/models/user_box.rb
1 belongs_to :user 2 belongs_to :box 3 validates :user_id, :box_id, presence: true
app/controllers/posts_controller.rb
1class PostsController < ApplicationController 2 before_action :set_current_user 3 4 def index 5 @others = User.where.not(id: current_user.id) 6 end 7 8 def new 9 @post = Post.new 10 end 11 12 def show 13 @other = User.find(params[:id]) 14 boxes = current_user.user_boxes.pluck(:box_id) 15 user_boxes = UserBox.find_by(user_id: @other.id, box_id: boxes) 16 17 unless user_boxes.nil? 18 @box = user_boxes.box 19 else 20 @box = Box.new 21 @box.save 22 UserBox.create(user_id: current_user.id, box_id: @box.id) 23 UserBox.create(user_id: @other.id, box_id: @box.id) 24 end 25 26 @posts = @box.posts.includes(:user).order('created_at desc') 27 @post = Post.new(box_id: @box.id) 28 end 29 30 def create 31 @post = Post.new(post_params) 32 if @post.save 33 flash[:notice] = "登録しました。" 34 redirect_to request.referer 35 end 36 end 37 38 def detail 39 @post = Post.find(params[:id]) 40 end 41 42 def edit 43 @post = Post.find(params[:id]) 44 end 45 46 def update 47 @post = Post.find(params[:id]) 48 @post.update(post_params) 49 if @post.save 50 flash[:notice] = "更新しました。" 51 redirect_to posts_path 52 else 53 flash[:alert] = "更新できませんでした。" 54 render("post/edit") 55 end 56 end 57 58 def destroy 59 Post.find(params[:id]).destroy 60 flash[:success] = "ファイルを削除しました。" 61 redirect_to posts_path 62 end 63 64 private 65 def post_params 66 params.require(:post).permit(:wake_up, :bathroom, :bathroom_time, :sleep, :pick_up, :pick_up_time, :date, :comment, :temperature, :box_id).merge(user_id: current_user.id) 67 end 68 69 def set_current_user 70 @user = current_user 71 end 72end
app/views/posts/index.html.erb
1<div class="form-signin"> 2 <h1 class="fw-bold">連絡帳</h1> 3 <h6>相手を選んでください</h6> 4 <% if current_user != @others %> 5 <% @others.each do |other| %> 6 <div class="row border-bottom pt-3"> 7 <%= link_to post_path(other.id), class:"plain" do %> 8 <div class="d-flex justify-content-center align-items-center"> 9 <div class="px-2"> 10 <% if other.picture.attached? %> 11 <%= image_tag other.picture.variant( resize: "75x75^", gravity: "center", crop: "75x75+0+0" ).processed, class: "rounded-circle" %> 12 <% else %> 13 <%= image_tag 'default_user_icon.png', class: "rounded-circle", style: "width: 75px; height: 75px;" %> 14 <% end %> 15 </div> 16 <div class="px-2 fs-3"> 17 <%= other.name %> 18 </div> 19 </div> 20 <% end %> 21 </div> 22 <% end %> 23 <% end %> 24 <div class="active text-center my-4"> 25 <%= button_to "Back", root_path, method: :get, class: "btn btn-outline-danger btn-lg fw-bold mx-3" %> 26 </div> 27</div>
あなたの回答
tips
プレビュー