簡體   English   中英

為什么我的表單只提交一個模型的數據?

[英]Why would my form submit only one model's data?

我有一個用於提交兩個模型的表單,第一個模型has_one另一個模型, Student_Detail

這里的許多用戶幫助我解決了一些我已經遇到的錯誤,現在我的表單成功提交,但只提交主要模型Participant

我已經提交了幾次表單,它存儲了作為Participant模型的直接屬性的所有信息。 在活動管理中, Participant模型數據填充,但不存在Student_Detail記錄。

在控制台中, Student_Detail數據不存在。

形式:

    <%= form_for @participant, url: {action: "create"} do |f| %>

                    <%= f.label :first_name, 'First:' %>
                    <%= f.text_field :first_name %>
                    <%= f.label :last_name, 'Last:' %>
                    <%= f.text_field :last_name %>
                    <%= f.label :gender, 'Sex:' %>                       
                    <%= f.select :gender, ['Male', 'Female'] %>
                    <br>
                    <br>
                    <%= f.label :birthdate, 'Birthdate:' %>
                    <%= f.date_field :birthdate %>
                    <%= f.label :email, 'Email:' %>
                    <%= f.text_field :email %>
                    <%= f.label :phone, 'Phone:' %>
                    <%= f.text_field :phone %>

                    <%= f.label :street_name, 'Street Number & Name:' %>
                    <%= f.text_field :street_name %>
                    <%= f.label :city, 'City:' %>
                    <%= f.text_field :city %>
                    <%= f.label :state, 'State:' %>
                    <%= f.text_field :state %>
                    <%= f.label :zip, 'Zip:' %>
                    <%= f.text_field :zip %>
            <!--        <%= f.label :role, 'Role:' %>
                    <%= f.select :role, ['Reader'] %> -->
                    <br>
                    <br>

         <%= f.fields_for @participant.student_detail do |student_detail_field| %>
                    <%= student_detail_field.label :nationality, 'Nationality:' %>  
                    <%= student_detail_field.text_field :nationality %>
                    <%= student_detail_field.label :religion, 'Religion:' %>
                    <%= student_detail_field.text_field  :religion  %>

                    <%= student_detail_field.label :need_ride, 'Do you need help getting to the church building?' %>
                    <%= student_detail_field.select :need_ride, ['Yes','No'] %> 
                    <br>
                    <br>
                    <%= student_detail_field.label :has_spouse, 'Marital Status:' %>
                    <%= student_detail_field.select :has_spouse, ['married', 'single'] %>
                    <br>
                    <br>
                    <%= student_detail_field.label :spouse_name, "If married, spouse's name:" %>
                    <%= student_detail_field.text_field  :spouse_name %>
                    <%= student_detail_field.label :english_level, 'English Level:' %>
                    <%= student_detail_field.select :english_level, ['Low', 'Medium Low', 'Medium', 'Medium High', 'High']  %>
                    <%= student_detail_field.label :expectation, 'What are your expectations for the program?:' %>
                    <%= student_detail_field.text_area :expectation %>
                    <%= student_detail_field.label :length_of_stay, 'About how long will you be in town?:' %>
                    <%= student_detail_field.select :length_of_stay, ['Less than 1 Year', '1 Year', '1-2 Years', '2-3 Years', '3+ Years'] %>
                    <%= student_detail_field.label :exact_length, 'Please tell us exactly how long you plan to be in town:' %>
                    <%= student_detail_field.text_area :exact_length %>
        <% end %>



楷模:

    class Participant < ApplicationRecord
    validates :last_name, presence: true
    # validates :gender, inclusion: { in: %w(male female) }
    validates :phone, presence: true
    has_one :volunteer_detail, :dependent => :destroy
      accepts_nested_attributes_for :volunteer_detail,   :allow_destroy => :true

    has_one :student_detail, :dependent => :destroy
      accepts_nested_attributes_for :student_detail,   :allow_destroy => :true

     end

——

     class StudentDetail < ApplicationRecord
belongs_to :participant
  end

控制器:

                    class ParticipantsController < ApplicationController
            def new
            @participant= Participant.new
            @studentdetail= @participant.build_student_detail(participant: @participant)
            end



            def create
            @participant = Participant.create(participant_params)
            @participant.save
            if @participant.save
            flash[:success] = "Successfully Registered!"        

            #email notifes admin of new registration
            NotifyMailer.notify_email(@participant).deliver_later

            redirect_to '/signup'
            end
            end

            def edit
            end

            def update
            end

            def show
            end

            def index
            end

            private
            def participant_params
            params.require(:participant).permit(:first_name, :last_name, :gender, :email, :birthdate, :phone, 
              :street_name, :city, :state, :zip, student_detail_attributes: [:nationality, :religion, :need_ride, 
            :has_spouse, :spouse_name, :english_level, :expectation, :length_of_stay, :exact_length, :volunteer_id, 
            :matched, :returned_home, :participant_id])

            end


            end

任何建議或意見表示贊賞,我試圖讓兩個模型在提交表單時填充數據,目前主要模型是唯一一個工作。

我相信這里的問題在於您視圖中 fields_for 行的語法。

更改f.fields_for @participant.student_detail do |student_detail_field|

f.fields_for :student_detail, @participant.student_detail do |student_detail_field|

我敢打賭,如果您在控制器中放入 binging.pry 或 puts 語句以查看您的參數,那么 student_detail_attributes 甚至不會到達您的參數。 您可以在控制器中插入puts params.inspect以檢查參數是否按您的預期通過。 您強大的 params 語法看起來和模型一樣正確,所以我認為這是您的觀點的問題。

編輯

這里有一些其他的事情可以嘗試。 正如我在評論中提到的,我通常不會這樣做

@participant = Participant.create(participant_params)

我只會做:

@participant = Participant.new(participant_params)
if @participant.save
#etc
end

上面的代碼也會稍微清理一下你的代碼。 (我還注意到您在此操作中沒有關於當參與者不保存時該怎么做的案例 - 作為旁注,您應該編寫一些代碼來處理該問題)

此外,如果以上這些都不起作用,有時我發現使用 accept_nested_attributes 我必須將模型設置為彼此的“逆”。 有關更多信息,請參閱這篇文章: https ://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through——它關於一個 has_many 但我認為它也應該與一個 has_one 相關——在參與者模型中嘗試:

has_one :student_detail, :dependent => :destroy, inverse_of: :participant

看看上面的任何一個(或兩者的組合)是否得到了嵌套模型的保存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM