如何使用fields_for从一种形式链接多个模型?

时间:2019-01-17 03:47:36

标签: ruby-on-rails activeadmin belongs-to nomethoderror has-one

我正在构建注册应用程序。共有三种模型:ParticipantVolunteer_DetailStudent_DetailParticipant has_one的其他两个模型,而它们依次为belongs_to Participant。我正在尝试使用带有fields_for的一种形式来向Participant和其他模型之一提供数据。

我总是遇到错误,说要用于Student_Detail的属性之一不是Participant的已定义属性。

我看过fields_for上的几本指南,似乎有几种方法可以定义表单要使用的第二个模型。

共识似乎是它应该像这样:

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

但是,当我编写:student_details而不是@ participant.student_details时,它似乎只显示我的表单。

这是我的表单,其中显示了顶部部分,以及包含fields_for的部分(修剪表单以节省空间):

                    <%= f.label :first_name, 'First:' %>
                    <%= f.text_field :first_name %>

         <%= f.fields_for :student_details do |student_detail_field| %>
                    <%= f.label :nationality, 'Nationality:' %> 
                    <%= student_detail_field.text_field :nationality %>

这是我的参与者控制者:

class ParticipantsController

            def new2
            @participant= Participant.new
            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, :nationality, :religion, :need_ride, 
            :has_spouse, :spouse_name, :english_level, :expectations, :length_of_stay, :exact_length, :volunteer_id, :matched, :returned_home, :participant_id, :participant_id_id)

            end


            end

这是服务器错误:

    NoMethodError (undefined method `nationality' for #<Participant:0x000000000d039410>
                            Did you mean?  Rational):

                            app/controllers/participants_controller.rb:11:in `create'
                            Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
                            Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
                            Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.9ms)
                            Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
                            Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
                            Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
                            Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
                            Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (790.6ms)

希望看看是否有人可以帮助阐明我的错误所在。

谢谢!

1 个答案:

答案 0 :(得分:1)

有些事情可能会有所帮助

  1. 如何定义已存在的关系

    rails guide之后,由于参与者has_one student_detail,因此Student_detail表必须具有字段partner_id,而自愿者_detail必须具有字段partner_id

    您可以设置participant.rb(模型)以接受student_detail或志愿者_detail的嵌套属性(因为您说过为这些表格提供一种形式)

    class Participant < ActiveRecord::Base
      has_one :student_detail, :dependent => :destroy
      accepts_nested_attributes_for :student_detail,   :allow_destroy => :true
      # that allow_destroy is depend with your software design
      has_one :volunteer_detail, :dependent => :destroy
      accepts_nested_attributes_for :volunteer_detail,   :allow_destroy => :true
    end
    
  2. 如何为其他表格设置 participant_params

    以下是定义用于接受嵌套属性的参与者参数的代码示例:

    def participant_params
      params.require(:participant).permit(
        :first_name, :other_participant_field_name, 
        student_detail_attributes: [:participant_id, :other_student_field_name],
        volunteer_detail_attributes: [:participant_id, :other_volunteer_field_name])
      end
    end
    

    如果您也想为volunteer_detail的学生记录内容,也可以在控制器中(创建方法),我的提示,请尝试重构/重新设计以最大程度地减少系统中的数据重复(学生和志愿者)

    def create
      @participant = Participant.create(participant_params)
      @volunteer_detail = @participant.student_detail
      # this will pass all the content of student detail to volunteer detail
      if @participant.save
        flash[:success] = "Successfully Registered!"        
      end
    end
    

要了解有关接受嵌套属性的更多信息,建议您向railcast 196学习,尽管它看起来很老,但是您可以学习这个概念