・問題点
掲載開始期間を設定できるようにしたのですが、その開始期間が始まったらトップページなどに表示、閲覧(show)できるようにしたいのですが、コントローラーにその機能を書くのか、viewページにそのような縛りで表示させるかなど、迷って困っています。
現状、コントローラーにdef plan_showを作成しましたので拝見して頂き、アドバイスを頂けたらと思っております、
・plans_controller
class PlansController < ApplicationController before_action :set_plan, only: [:show, :edit, :update, :destroy ] before_action :require_user_logged_in before_action :current_user, only: [:destroy, :edit, :new] before_action :require_user_logged_in, only: [:edit] before_action :plan_destroy, only: [:show, :index] def index @pagy, @plans = pagy(Plan.order(id: :desc)) end def show end def new @plan = Plan.new if @plan.user == current_user redirect_to '/' end end def create @plan = current_user.plans.build(plan_params) if @plan.save flash[:success] = '企画掲載を設定しました' redirect_to root_url else @pagy, @plans = pagy(current_user.plans.order(id: :desc)) flash.now[:danger] = '企画掲載に失敗しました' render 'plans/new' end end def destroy @plan = Plan.find_by(id: params[:id]) @plan.destroy flash[:success] = '掲載を中止しました。' redirect_to("/") end private def set_plan @plan = Plan.find(params[:id]) end def plan_params params.require(:plan).permit(:title, :photo, :detail, :start_on, :end_on) end def plan_destroy Plan.where('end_on < ?', Date.today).destroy_all end def plan_show Plan.where('start_on < ?', Date.today) end def correct_user @plan = current_user.plans.find_by(id: params[:id]) unless @plan redirect_to root_url end end end
・view/toppages/index
class ToppagesController < ApplicationController def index @all_plan = Plan.all @user = User.all @plans = Plan.order(created_at: :desc).limit(5) if logged_in? @my_plans = current_user.plans.order(created_at: :desc).limit(5) end end end
・toppages_controller
class ToppagesController < ApplicationController def index @all_plan = Plan.all @user = User.all @plans = Plan.order(created_at: :desc).limit(5) if logged_in? @my_plans = current_user.plans.order(created_at: :desc).limit(5) end end end
・views/plans/show.html.erb
<% if logged_in? %> <% if current_user == @plan.user %> <div class='plan-content'> <h4><%= @plan.title %></h4> <p><%= @plan.start_on %>~<%= @plan.end_on %></p> <p><%= image_tag @plan.photo.url, :alt => '企画書イメージ図' if @plan.photo? %></p> <p><%= @plan.detail %></p> <div class="plan-content-btn"> <%= link_to "削除", @plan, method: :delete, data: { confirm: "削除しますか?" } %> <%= link_to '戻る', '/' %> </div> </div> <% else %> <div class='plan-content'> <h4><%= @plan.title %></h4> <p><%= @plan.start_on %>~<%= @plan.end_on %></p> <p><%= image_tag @plan.photo.url, :alt => '企画書イメージ図' if @plan.photo? %></p> <p><%= @plan.detail %></p> <div class="plan-content-btn"> <%= link_to '応募する', '/' %> <%= link_to '戻る', '/' %> </div> </div> <% end %> <% else %> <div class='plan-content'> <h4><%= @plan.title %></h4> <p><%= @plan.start_on %>~<%= @plan.end_on %></p> <p><%= image_tag @plan.photo.url, :alt => '企画書イメージ図' if @plan.photo? %></p> <p><%= @plan.detail %></p> <div class="plan-content-btn"> <%= link_to '応募する', login_path %> <%= link_to '戻る', '/' %> </div> </div> <% end %>
情報が物足りない箇所がありましたら、また回答から聞いていただければ幸いです。
お手数をお掛けしてしまい、大変申し訳ありませんが、拝見して頂いて、アドバイスを頂けたらなと思っております。

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
退会済みユーザー
2022/09/16 08:02
2022/09/16 11:32
2022/09/16 11:33
退会済みユーザー
2022/09/19 14:51
2022/09/20 08:11