Railsでのメモアプリの新規登録機能の実装ができない
Q&A
Closed
解決したいこと
Railsでのメモアプリの新規登録機能が実装できない。
例)
Ruby on Railsでラジオ番組の放送内容や感想をメモするアプリをつくっています。
番組内容、感想をメモする機能の前情報として、パーソナリティ名や放送局、放送時間を登録しておく番組情報の登録機能を実装中にエラーが発生しました。
解決方法を教えて下さい。
発生している問題・エラー
ラジオ番組情報の登録の際、パーソナリティ名、放送局、放送日時、開始時刻、終了時刻の必要項目を埋めて番組情報を登録しようとしても、番組情報の新規登録はできずに番組情報の新規登録画面に遷移してしまう。
該当するソースコード
class ProgramsController < ApplicationController
before_action :set_program, only: %i[show edit update destroy]
def index
@q = Program.ransack(params[:q])
@programs = @q.result(distinct: true).sort_by { |program| [week_day_order(extract_day_of_week(program.day_of_week)), program.start_time] }
end
def new
@programs = Program.all.order(day_of_week: :asc)
@programs = @programs.sort_by { |program| week_day_order(program.day_of_week) }
end
def create
@program = Program.new(program_params)
if @program.save
host = Host.find_or_create_by(name: @program.host_name)
@program.hosts << host unless @program.hosts.include?(host)
@program.update(host_id: host.id)
redirect_to programs_path, success: "番組情報を作成しました"
else
flash.now[:danger] = "番組情報を作成できませんでした"
render :new, status: :unprocessable_entity
end
end
def show; end
def edit; end
def update
if @program.update(program_params)
redirect_to @program, success: "番組情報を更新しました"
else
flash.now[:danger] = "番組情報を更新できませんでした"
render :edit, status: :unprocessable_entity
end
end
def destroy
@program.destroy!
redirect_to programs_path, success: "番組情報を削除しました", status: :see_other
end
private
def program_params
params.require(:program).permit(:title, :host_name, :channel, :day_of_week, :start_time, :end_time, :host_id)
end
def set_program
@program = Program.find(params[:id])
end
def extract_day_of_week(day_of_week)
day_of_week.match(/(月曜日|火曜日|水曜日|木曜日|金曜日|土曜日|日曜日)/)&.to_s
end
def week_day_order(day_of_week)
days = %w[月曜日 火曜日 水曜日 木曜日 金曜日 土曜日 日曜日]
days.index(day_of_week) || 0
end
end
new.html.erb
<h1 class="font-bold mb-6 text-center" style="font-size: 2.5rem;">
番組情報登録
</h1>
<%= render 'form', program: @program %>
_form.html.erb
<%= form_with model: @program, local: true do |f| %>
<div class='field mb-4'>
<%= f.label "番組名", class: "form-label" %>
<%= f.text_field :title, class: "input input-bordered w-full max-w-xl" %>
</div>
<div class='field mb-4'>
<%= f.label "パーソナリティ名", class: "form-label" %>
<%= f.text_field :host_name, class: "input input-bordered w-full max-w-xl" %>
</div>
<div class='field mb-4'>
<%= f.label "放送局", class: "form-label" %>
<%= f.text_field :channel, class: "input input-bordered w-full max-w-xl" %>
</div>
<div class='field mb-4'>
<%= f.label "放送曜日", class: "form-label" %>
<%= f.text_field :day_of_week, class: "input input-bordered w-full max-w-xl" %>
</div>
<div class="max-w-[16rem] mx-auto grid grid-cols-2 gap-4">
<div class='field mb-4'>
<%= f.label :start_time, "放送開始時刻", class: "form-label" %>
<%= f.time_field :start_time,
class: "bg-gray-50 border border-gray-300 leading-none text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5",
style: "border-color: #d1d5db;",
required: true %>
</div>
<div class='field mb-4'>
<%= f.label :end_time, "放送終了時刻", class: "form-label" %>
<%= f.time_field :end_time,
class: "bg-gray-50 border border-gray-300 leading-none text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5",
style: "border-color: #d1d5db;",
required: true %>
</div>
</div>
<div class="flex justify-center mb-6">
<%= f.submit "登録", class: 'btn bg-orange-500 hover:bg-orange-600 text-white' %>
</div>
<% end %>
自分で試したこと
ここに問題・エラーに対して試したことを記載してください。
申し訳ありませんが、何がダメなのか見当もつきません。ヒントでもいいのできっかけをいただきたいです。
0