Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

問い合わせフォームから日程予約できるようにしたい

やりたいこと

お問い合わせフォームから日程予約ができるようにしたいです。

カレンダーからGUIでカレンダーの日程を選べるようにしていて、simple_calendarというgemを使っています。

何につまづいているか

確認画面から、createアクションを実行してレコードを作成しようとしています。
その際、accepts_nested_attributes_forを使って、contactテーブル、reservationテーブルにそれぞれのレコードを作成したいと思いました(同メソッドが非推奨なのは承知しております)
しかしながら、確認画面で送信ボタンをおすと、以下のようになってしまい、Reservationモデルが許可されていないと出てきてしまいます。

  Parameters: {"authenticity_token"=>"[FILTERED]", "contact"=>{"name"=>"test", "name_hiragana"=>"いしだけいすけ", "email"=>"test@gmail.com", "matter"=>"お問い合わせ", "menu"=>"test", "payment"=>"クレジットカード決済", "reservation"=>{"day"=>"2022-07-09", "time"=>"16:00"}, "content"=>"さ"}, "commit"=>"送信"}
Unpermitted parameter: :reservation

こちらでreservation_attributesと記述しているのですが、上手くいかずネットの記事を見てもどれも上手くいきませんでした。

contact_controller.rb
  def contact_reservation_params
    params.require(:contact).permit(
      :name, :name_hiragana, :email, :matter, :menu, :discount, :payment, :content,
      reservation_attributes: [
        :day, :time
    ])
  end

分かる方がいらっしゃいましたら、ご教示いただけると幸いです

コード

contact_controller.rb
class ContactsController < ApplicationController
  def new
    @contact = Contact.new  
  end
  
  def calendar
    @contact = Contact.new(contact_params)
    render 'new' if @contact.invalid?
    @reservations = Reservation.all.where('day >= ?', Date.current)
    session[:contact] = params[:contact]
  end

  def reservation
    @day = params[:day]
    @time = params[:time]
    @reservation = Reservation.new
    if @day.to_date <= Date.today
      redirect_to new_contacts_path, alert: '過去の日付が入力されたので、最初からやり直してください'
    end
  end
             
  def confirm
    @reservation = Reservation.new(reservation_params)
    @contact = Contact.new(session[:contact])
  end

  def create
    @contact = Contact.new(contact_reservation_params)
    @contact.build_reservation

    if @contact.save
      redirect_to finish_contacts_path
    else
      redirect_to new_contacts_path, alert: '何らかのエラーが発生しました。申し訳ございませんが、内容をご確認の上最初からやり直してください。'
    end
  end

  def finish
  end

  private
  
  def contact_params
    params.require(:contact).permit(:name, :name_hiragana, :email, :matter, :menu, :discount, :payment, :content)
  end

  def reservation_params
    params.require(:reservation).permit(:day, :time)
  end

  def contact_reservation_params
    params.require(:contact).permit(
      :name, :name_hiragana, :email, :matter, :menu, :discount, :payment, :content,
      reservation_attributes: [
        :day, :time
    ])
  end
end
contact.rb
class Contact < ApplicationRecord
  # 定数
  VALIDATE_FURIGANA = /[ぁ-ん]/u
  VALIDATE_EMAIL = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  # アソシエーション
  belongs_to :reservation, foreign_key: "reservation_id", optional: true


  # バリデーション
  validates :name,                  presence: true
                                    # length: {maximum: 255}
  
  validates :name_hiragana,         presence: true,
                                    length: { maximum: 255},
                                    format: { with: VALIDATE_FURIGANA, allow_blank: true }

  validates :email,                 presence: true,
                                    length: {maximum: 255},
                                    format: { with: VALIDATE_EMAIL, allow_blank: true }

end
reservation.rb
class Reservation < ApplicationRecord
  # アソシエーション
  has_one :contact
  accepts_nested_attributes_for :contact
end
confirm.html.erb
<% provide(:title, 'お問い合わせ | 確認') %>

<%= render partial: '/shared/header' %>

<%= form_with(model: @contact, url: contacts_path, local: true) do |f| %>
<div class='container-fluid text-center w-75' id='content'>
  <div class='mt-2 mb-5 mr-5'>
    <h2 class='display-4'>お問い合わせ確認</h2>
  </div>
  <form>
    <div class='form-group row'>
      <%= f.label :name, class: 'col-sm-2 col-form-label' %>
      <div class='col-sm-10'>
        <%= f.text_field :name, class: 'form-control form-control-lg col-sm-8 text-center', readonly: true %>
      </div>
    </div>
    <div class='form-group row mt-5'>
      <%= f.label :name_hiragana, class: 'col-sm-2 col-form-label' %>
      <div class='col-sm-10'>
        <%= f.text_field :name_hiragana, class: 'form-control form-control-lg col-sm-8 text-center', readonly: true %>
      </div>
    </div>
    <div class='form-group row mt-5'>
      <%= f.label :email, class: 'col-sm-2 col-form-label' %>
      <div class='col-sm-10'>
        <%= f.text_field :email, class: 'form-control form-control-lg col-sm-8 text-center', readonly: true %>
      </div>
    </div>
    <div class='form-group row mt-5'>
      <%= f.label :matter, class: 'col-sm-2 col-form-label' %>
      <div class='col-sm-10'>
        <%= f.text_field :matter, class: 'form-control form-control-lg col-sm-8 text-center', readonly: true %>
      </div>
    </div>
    <div class='form-group row mt-5'>
      <%= f.label :menu, class: 'col-sm-2 col-form-label' %>
      <div class='col-sm-10'>
        <%= f.text_field :menu, class: 'form-control form-control-lg col-sm-8 text-center', readonly: true %>
      </div>
    </div>
    <div class='form-group row mt-5'>
      <%= f.label :payment, class: 'col-sm-2 col-form-label' %>
      <div class='col-sm-10'>
        <%= f.text_field :payment, class: 'form-control form-control-lg col-sm-8 text-center', readonly: true %>
      </div>
    </div>
    <%= f.fields_for :reservation do |reservation| %>
      <div class='form-group row mt-5'>
      <%= f.label :payment, class: 'col-sm-2 col-form-label' %>
        <div class='col-sm-10'>
          <% if @reservation.day.nil? %>
            <%= reservation.text_field :day, class: 'form-control form-control-lg col-sm-8 text-center', value: 'なし', readonly: true %>
          <% else %>
            <%= reservation.text_field :day, class: 'form-control form-control-lg col-sm-8 text-center', value: @reservation.day, readonly: true %>
          <% end %>
        </div>
      </div>
      <div class='form-group row mt-5'>
      <%= f.label :time, class: 'col-sm-2 col-form-label' %>
        <div class='col-sm-10'>
          <% if @reservation.time.nil? %>
            <%= reservation.text_field :time, class: 'form-control form-control-lg col-sm-8 text-center', value: 'なし', readonly: true %>
          <% else %>
            <%= reservation.text_field :time, class: 'form-control form-control-lg col-sm-8 text-center', value: @reservation.time, readonly: true %>
          <% end %>
        </div>
      </div>
    <% end %>
    <div class='row mt-5'>
      <%= f.label :content, class: 'col-sm-2 col-form-label' %>
      <div class='col-sm-7'>
        <%= f.text_area :content, class: 'form-control', rows: '10', readonly: true%>
      </div>
    </div>
  </form>
  <%= f.submit value: '送信', class: 'btn btn-primary btn-lg mt-5' %>
</div>
<% end %>
0

1Answer

Your answer might help someone💌