Closed kodamarisa closed 4 months ago
先日アプリを触ったときの記憶しかないため違っていたらすみません。 個々のカレンダーのidは (URL:...../37)とか、params[:id]で表示していたかと思うので、フォームを使ってそれを上手く処理に含められているのか、params[:id]を上手く使ってカラムに保存できないかなど確認してみてください。
わかりました、やってみます。
URLがhttp://localhost:3000/schedules/new?calendar_id=3&date=2024-05-24
のようになっているのでformにhidden_fieldを使ってカレンダーID用の要素を追加してあげてもいいかもですね
<h2>New Schedule for <%= params[:date] %></h2>
<%= form_with(model: @schedule, local: true) do |form| %>
<%= form.hidden_field :calendar_id, value: params[:calendar_id] %>
<div class="field">
<%= form.label :date %>
<%= form.date_field :date, value: params[:date] %>
</div>
<div class="field">
<%= form.label :exercise_id, "Exercise" %>
<%= form.collection_select :exercise_id, @exercises, :id, :name, prompt: "Choose an exercise" %>
</div>
<div class="field">
<%= form.label :repetitions %>
<%= form.number_field :repetitions %>
</div>
<div class="field">
<%= form.label :duration, "Duration (seconds)" %>
<%= form.number_field :duration %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
コメントありがとうございます。 ただいま、カレンダーにスケジュールをネストして動作がうまくいくか試しているところです。
上記のネストして動きを確認しているのですが
# Calendar routes
resources :calendars, except: [:destroy, :update] do
resources :schedules, only: [:index, :show, :new]
end
この状態で、スケジュールが表示されない状態です。
# Calendar routes
resources :calendars, except: [:destroy, :update] do
resources :schedules, only: [:index, :show, :new]
end
コレと
class SchedulesController < ApplicationController
before_action :set_calendar
before_action :set_schedule, only: [:show, :edit, :update, :destroy]
def new
@schedule = @calendar.schedules.build
@exercises = Exercise.all
end
def create
@schedule = @calendar.schedules.build(schedule_params)
if @schedule.save
redirect_to calendar_path(@calendar), notice: 'Schedule was successfully created.'
else
@exercises = Exercise.all
render :new
end
end
def show
end
def edit
@exercises = Exercise.all
end
def update
if @schedule.update(schedule_params)
redirect_to calendar_path(@calendar), notice: 'Schedule was successfully updated.'
else
@exercises = Exercise.all
render :edit
end
end
def destroy
@schedule.destroy
redirect_to calendar_path(@calendar), notice: 'Schedule was successfully deleted.'
end
private
def set_schedule
@schedule = @calendar.schedules.find(params[:id])
end
def schedule_params
params.require(:schedule).permit(:date, :exercise_id, :repetitions, :duration)
end
end
コレと
<h2>New Schedule for <%= params[:date] %></h2>
<%= form_with(model: [@calendar, @schedule], local: true) do |form| %>
<div class="field">
<%= form.label :date %>
<%= form.date_field :date, value: params[:date] %>
</div>
<div class="field">
<%= form.label :exercise_id, "Exercise" %>
<%= form.collection_select :exercise_id, @exercises, :id, :name, prompt: "Choose an exercise" %>
</div>
<div class="field">
<%= form.label :repetitions %>
<%= form.number_field :repetitions %>
</div>
<div class="field">
<%= form.label :duration, "Duration (seconds)" %>
<%= form.number_field :duration %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
コレと
<div class="calendar-header">
<h1>Calendars#show</h1>
</div>
<div class="calendar-header">
<% if @customize.present? && @customize.image.present? %>
<%= image_tag @customize.image.url, class: 'calendar-image' %>
<% else %>
<p>No image available</p>
<% end %>
</div>
<%= content_tag :div, class: "simple-calendar #{session[:calendar_color]}" do %>
<%= month_calendar events: @events do |date, events| %>
<div class="calendar-date">
<%= link_to date.day, new_calendar_schedule_path(calendar_id: @calendar.id, date: date), class: 'date-link' %>
</div>
<div class="event-list">
<% events.each do |event| %>
<div class="event-item">
<%= link_to event.exercise.name, schedule_path(event), class: 'exercise-link' %>
</div>
<% end %>
</div>
<% end %>
<% end %>
この状態なのですが、ルーティングが問題だと考えていいでしょうか?
表示されない原因がわからないのでスクリーンショットや検証ツールのコンソール上に出ているエラーやサーバーログなども合わせて共有してください
Started GET "/calendars/30" for ::1 at 2024-05-24 20:46:36 +0900
Processing by CalendarsController#show as HTML
Parameters: {"id"=>"30"}
Calendar Load (1.3ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = $1 LIMIT $2 [["id", 30], ["LIMIT", 1]]
↳ app/controllers/calendars_controller.rb:52:in `set_calendar'
Customize Load (1.7ms) SELECT "customizes".* FROM "customizes" WHERE "customizes"."calendar_id" = $1 LIMIT $2 [["calendar_id", 30], ["LIMIT", 1]]
↳ app/controllers/calendars_controller.rb:10:in `show'
Rendering layout layouts/application.html.erb
Rendering calendars/show.html.erb within layouts/application
Schedule Load (1.8ms) SELECT "schedules".* FROM "schedules" WHERE "schedules"."calendar_id" = $1 [["calendar_id", 30]]
↳ app/views/calendars/show.html.erb:14
Rendered /Users/kodamarisa/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/simple_calendar-3.0.4/app/views/simple_calendar/_month_calendar.html.erb (Duration: 7.4ms | Allocations: 8232)
Rendered calendars/show.html.erb within layouts/application (Duration: 13.6ms | Allocations: 10258)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_header.html.erb (Duration: 0.6ms | Allocations: 116)
Rendered layouts/_sidebar.html.erb (Duration: 0.9ms | Allocations: 422)
Rendered layout layouts/application.html.erb (Duration: 30.2ms | Allocations: 16971)
Completed 200 OK in 49ms (Views: 33.1ms | ActiveRecord: 4.8ms | Allocations: 20045)
Started GET "/schedules/new?date=undefined&calendar_id=undefined" for ::1 at 2024-05-24 20:46:37 +0900
ActionController::RoutingError (No route matches [GET] "/schedules/new"):
上記がターミナルで見られるログとなっています。 スクショが検証ツールでの状態です https://gyazo.com/28f7355dfefe2d70ebabede7a85e44a4
すみません、スケジュール出るようになりました、
document.addEventListener("DOMContentLoaded", () => {
const modal = document.getElementById("schedule-modal");
const span = document.getElementsByClassName("close")[0];
document.querySelectorAll(".calendar-date").forEach(element => {
element.addEventListener("click", (event) => {
event.preventDefault();
const date = event.currentTarget.dataset.date;
const calendarId = event.currentTarget.dataset.calendarId;
fetch(`/calendars/${calendarId}/schedules/new?date=${date}`) // ルーティングに合わせてURLを変更
.then(response => response.text())
.then(html => {
document.getElementById("schedule-details").innerHTML = html;
modal.style.display = "block";
});
});
});
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
});
これのurlを変更していなかったため動かなかったんだと思います!
動くようになって良かったです。問題はクリアという認識で良いでしょうか?
そうですね、とりあえず動作の確認となります。
該当のソースコード
New Schedule for <%= params[:date] %>
<%= form_with(model: @schedule, local: true) do |form| %>
<% end %>
class CalendarsController < ApplicationController before_action :set_calendar, only: [:show, :edit, :update, :destroy] before_action :authenticate_user_or_line_user!, except: [:index, :show, :new, :create, :edit, :update, :destroy]
def index @calendars = Calendar.all end
def show @customize = Customize.find_by(calendar_id: @calendar.id) if @calendar @events = @calendar.schedules.includes(:exercise) else render file: "#{Rails.root}/public/404.html", layout: false, status: :not_found end expires_now end
def new @calendar = Calendar.new end
def create @calendar = Calendar.new(calendar_params) if @calendar.save handle_successful_save else render :new end end
def edit @calendar = Calendar.find(params[:id]) end
def update if @calendar.update(calendar_params) redirect_to calendars_path, notice: 'Calendar was successfully updated.' else render :edit end end
private
def set_calendar @calendar = Calendar.find_by(id: params[:id]) unless @calendar @calendar = Calendar.create(title: "Default Calendar") session[:current_calendar_id] = @calendar.id end end
def calendar_params params.require(:calendar).permit(:title, :image) end
def handle_successful_save if @calendar session[:current_calendar_id] = @calendar.id add_current_user_to_calendar if user_signed_in? redirect_to calendar_path(@calendar), notice: 'Calendar was successfully created.' else redirect_to calendars_path, alert: 'Error creating calendar.' end end
def add_current_user_to_calendar @calendar.users << current_user end
def add_current_line_user_to_calendar @calendar.line_users << current_line_user end end