質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

1499閲覧

simple_calendarを使って予定を出力させたい

gomappi

総合スコア7

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/03/20 11:05

編集2021/03/20 11:15

前提・実現したいこと

ただいま、カレンダーのアプリを作成しています。
railsのgemのsimple_calendarを使ってカレンダーを作成しているのですが、予定を入力し作成するとカレンダーに出力できるようにしたいのですが、うまく出力されません。
データベース上には保存ができているのですが表示だけができていない状態です。
ご教授いただけると助かります。イメージ説明

該当のソースコード

views/calendars/show.html.erb(カレンダー表示のビュー)

<h1><%= "#{@calendar.calendar_name}のカレンダー" %></h1> <%= link_to '予定を作成', new_calendar_schedule_path(@calendar.id) %> <%= month_calendar events: @schedules do |date,schedules| %> <%= date.day %> <% schedules.each do |schedule| %> <%= schedule.schedule_name %> <% end%> <% end%>

views/schedules/new.html.erb(スケジュール作成のビュー)

<h1>スケジュール作成</h1> <%= form_with model: @schedule, url: calendar_schedules_path, method: :post, local: true do |f| %> <%= render 'shared/error_messages', model: f.object %> 日程 <%= f.date_select :date %> スケジュール名 <%= f.text_field :schedule_name %> ジャンル <%= f.collection_select(:genre_id, Genre.all, :id, :name, {}, {}) %> 開始時間 <%= f.time_select(:start_time, prompt:'--',:minute_step => 5,time_zone: 'Tokyo') %> 終了時間 <%= f.time_select(:end_time, prompt:'--',:minute_step => 5) %> コメント <%= f.text_area :comment %> <%= f.submit '作成' %> <% end %>

views/simple_calendar/_month_calendar.html.erb(simple_calendarのビュー)

<div class="simple-calendar"> <div class="calendar-heading"> <%= link_to t('simple_calendar.previous', default: '前の月'), calendar.url_for_previous_view %> <span class="calendar-title"><%= start_date.year %>年 <%= t('date.month_names')[start_date.month] %> </span> <%= link_to t('simple_calendar.next', default: '次の月'), calendar.url_for_next_view %> </div> <table class="table table-striped"> <thead> <tr> <% date_range.slice(0, 7).each do |day| %> <th><%= t('date.abbr_day_names')[day.wday-1] %></th> <% end %> </tr> </thead> <tbody> <% date_range.each_slice(7) do |week| %> <tr> <% week.each do |day| %> <%= content_tag :td, class: calendar.td_classes_for(day-1) do %> <% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(passed_block) %> <% capture_haml(day, sorted_events.fetch(day, []), &passed_block) %> <% else %> <% passed_block.call day-1, sorted_events.fetch(day, []) %> <% end %> <% end %> <% end %> </tr> <% end %> </tbody> </table> </div>

models/schedule.rb

class Schedule < ApplicationRecord before_save :set_time_zone belongs_to :user belongs_to :calendar with_options presence:true do validates :date validates :schedule_name validates :genre_id ,numericality: {other_than: 1} validates :user_id validates :calendar_id end def set_time_zone year = self.date.year month = self.date.month day = self.date.day self.start_time = self.start_time.change(year: year, month: month, day: day) self.end_time = self.end_time.change(year: year, month: month, day: day) end def start_time self.date end end

scheduleのマイグレーションファイル

class CreateSchedules < ActiveRecord::Migration[6.0] def change create_table :schedules do |t| t.references :user, null: false, foreign_key: true t.references :calendar, null: false, foreign_key: true t.date :date, null: false t.string :schedule_name, null: false t.integer :genre_id, null: false t.time :start_time t.time :end_time t.text :comment t.timestamps end end end

controllers/calendars_controller

class CalendarsController < ApplicationController before_action :authenticate_user!, only: [:new, :create, :show] def index @calendars = Calendar.all end def new @calendar = Calendar.new end def create @calendar = Calendar.new(calendar_params) if @calendar.save redirect_to root_path else render :new end end def show @calendar = Calendar.find(params[:id]) @schedules = Schedule.all end private def calendar_params params.require(:calendar).permit(:calendar_name).merge(user_id: current_user.id) end end

試したこと

下記のサイトを参考に作成いたしました。
【rails】simple_calendarを使ってカレンダーつきのブログ機能を作ってみた。

補足情報(FW/ツールのバージョンなど)

ruby 2.6.5
Rails 6.0.3.5

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問