閲覧頂きまして、ありがとうございます。
railsで、ユーザーが投稿したコンテンツの表示を管理者が承認するまで非表示にしたいと考えています。
考えているやり方は、コントローラー上でcontentのカラム valid_flag
がtrueでないと表示されないような命令を出したいと考えています。この条件文の書き方が調べても、なかなか分からなくて困っています。
また、別のおすすめのやり方がございましたら、ご教示いただければ幸いです。
ソースコード
Ruby
1class ComtentsController < ApplicationController 2 before_action :set_content, only:[:show, :edit, :update] 3 # アクションの前にvalid_contentを起動する 4 before_action :valid_content, only: [:show, edit,:update] 5 before_action :correct_user, only:[:edit] 6 7 def new 8 if user_signed_in? 9 @content = current_user.contents.build 10 else 11 redirect_to root_path 12 end 13 end 14 15 def create 16 @content = current_user.contents.create(create_params) 17 if @content.save 18 redirect_to root_path 19 else 20 redirect_to root_path 21 end 22 end 23 24 def edit 25 end 26 27 def show 28 end 29 30 private 31 32 def valid_content 33 @content = Content.find(params[:id]) 34 end 35 36 def valid_content 37 # ここにvalid_flagがtrueでない場合しか表示しない命令を書きたい 38 end 39 40 def create_params 41 params.require(:content).permit(:id, :content, :user_id, :valid_flag) 42 end 43 44 def correct_user 45 if user_signed_in? 46 @content = current_user.contents.find_by(id: params[:id]) 47 redirect_to root_path if @content.nil? 48 else 49 redirect_to root_path 50 end 51 end 52end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/23 03:38