check_boxヘルパーを使用して、createしたい。
railsで、登録した患者をeach doで一覧表示させ、その一覧からcheck_boxヘルパーを使用して、同ページでチェックが入った患者さんだけを表示するという機能を実装しようとしています。
<img src="https://gyazo.com/450b5e55306c02c7e87639da123b8c9c">
発生している問題・エラーメッセージ
ArgumentError in PatientListsController#create wrong number of arguments (given 0, expected 1..2) Extracted source (around line #21): def patient_list_params #21 params.require(:patient_list).permit(:list[]).merge(nurse_id: current_nurse.id, patient_id: params[:patient_id]) end end
該当のソースコード
route.rb
Rails.application.routes.draw do devise_for :nurses, controllers: { sessions: 'nurses/sessions', passwords: 'nurses/passwords', registrations: 'nurses/registrations' } devise_for :patients, controllers: { sessions: 'patients/sessions', passwords: 'patients/passwords', registrations: 'patients/registrations' } get 'questions/index' root to: "questions#index" resources :questions, only: [:index, :new, :create] resources :checkups, only: [:new, :create] resources :symptoms, only: [:new, :create] resources :medical_treatments, only: [:new, :create] resources :patient_lists, only: [:index, :new, :create] end
controllers/patient_lists_controller.rb
class PatientListsController < ApplicationController def index wd = ["日", "月", "火", "水", "木", "金", "土"] time = Time.now @date = time.strftime("%Y/%m/%d(#{wd[time.wday]})") @patient_list = PatientList.new @patients = Patient.all.order(:name) end def new end def create PatientList.create(patient_list_params) end private def patient_list_params params.require(:patient_list).permit(:list[]).merge(nurse_id: current_nurse.id, patient_id: params[:patient_id]) end end
model/patient_list.rb
class PatientList < ApplicationRecord validates :list, presence: true end
views/patient_lists/index.html.erb
<div class = "patient_list_contents"> <div class = "side_bar"> <%= render "side_bar" %> </di/v> <div class="v_line_fix"></div> <div class = "main"> <%= render "main_list" %> </div> </div>
views/patient_lists/_side_bar.html.erb
<div class="side-bar-header"> <div class = "date"><%= @date %></div> </div> <div class="patient_list_room"> <div class="patient_lists"> <div class="title-border"> <h2>患者一覧</h2> </div> <div class="patient_list"> <%= form_with model: @patient_list, local: true do |f| %> <% @patients.each do |patient| %> <div class="patient"> <ul> <li><%= f.check_box :list, {multiple: true}, patient.name, nil %><%= patient.name%></li> </ul> </div> <% end %> <div class="checkup_content-submit"> <%= f.submit "確認する" , class:"btn" %> </div> <% end %> </div> </div> </div>
class CreatePatientLists < ActiveRecord::Migration[6.0] def change create_table :patient_lists do |t| t.references :patient, null: false, foreign_key: true t.references :nurse, null: false, foreign_key: true t.string :list, null:false t.timestamps end end end
試したこと
エラー文は期待値が1、2個あるはずだが、実際には0個である、という意味かと思います。
<ActionController::Parameters {"authenticity_token"=>"9PlSXw+VlA0+7CU+khuT1MLd/Op7WDnaLwbVGdyNedVds8a6gWw92YnfN4eShHpU4kEGWKt3Kt5KOZ0GmoWsAA==", "patient_list"=>{"list"=>["佐藤 花子", "山田 太郎"]}, "commit"=>"確認する", "controller"=>"patient_lists", "action"=>"create"} permitted: false>
と値は入っているようなのですが、期待している機能がうまく実装できない状態です。
恐縮ですが、ご教示頂ければと思います。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー