简体   繁体   English

Rails:嵌套模型表单未创建记录

[英]Rails: Nested model form not creating record

I'm trying to create a form that can both update an 'Invite' and add a new 'Guest' (a nested form). 我正在尝试创建一个既可以更新“邀请”又可以添加新的“来宾”(嵌套表单)的表单。 An 'Invite' has_many 'Guests' and 'Guests' belong_to 'Invites'. “邀请”具有多个“来宾”和“来宾”属于“邀请”。 I've gone through setting up accepts_nested_attributes_for but I cannot get the form to add a new 'Guest'. 我已经完成了accepts_nested_attributes_for设置,但无法获得添加新“来宾”的表单。

At the moment the from can update the Invite but just directs to the 'show' view, but does not add a new Guest record. 目前,发件人可以更新“邀请”,而只是直接转到“显示”视图,而不会添加新的来宾记录。 and I'm not sure why. 我不知道为什么。 I have a feeling it has something to do with the Invite 'update' method... 我觉得这与邀请'更新'方法有关...

Here's some code: 这是一些代码:

Invite model: 邀请模型:

class Invite < ActiveRecord::Base
  has_many :guests
  def invite_params

  def invite_params
    params.require(:invite).permit(:invite_code, :name)
  end

  accepts_nested_attributes_for :guests

  validates :invite_code, presence: true, numericality: true, length: { is: 4 }
  validates :name, presence: true, length: { minimum: 5 }
 end

Guest model: 访客模型:

class Guest < ActiveRecord::Base
  belongs_to :invite

  def guest_params
    params.require(:guest).permit(:name, :attendance_status, :starter, :main, :desert, :dietary_requirements)
  end

  validates :name, presence: true, length: { minimum: 5 }
  validates :attendance_status, presence: true
end

Form: 形成:

<%= form_for @invite, url: invite_path(@invite), method: :patch do |invite_form| %>
  <p>
    <%= invite_form.label :name %><br>
    <%= invite_form.text_field :name %>
  </p>

  etc...

  <%= invite_form.fields_for :guest do |guest_form| %>
    <h3>Add a Guest</h3>
    <p>
      <%= guest_form.label :name %><br>
      <%= guest_form.text_field :name %>
    </p>

    etc...
  <% end %>

  <%= invite_form.submit 'Update' %>

<% end %>

Guest controller: 访客控制器:

class GuestsController < ApplicationController
 def new
   @invite = Invite.find(params[:invite_id])
   @guest = @invite.guests.build
 end

 def create
   @invite = Invite.find(params[:invite_id])
   @guest = @invite.guests.create(guest_params)
   redirect_to edit_invite_path(@invite)
 end

 etc...

Invite controller: 邀请控制器:

def update
 @invite = Invite.find(params[:id])

 if @invite.update(invite_params)
   redirect_to @invite
 else
   render 'edit'
 end
end

I'm guessing this is something to do with the controller method but I can't figure out what? 我猜想这与控制器方法有关,但我不知道是什么?

Any help appreciated... 任何帮助表示赞赏...

Firstly, you need to move your params declarations to your invite controller, where Rails 4 intends them to be. 首先,您需要将params声明移至邀请控制器,Rails 4打算将其声明为。

Your params will need to take the nested attributes in a single params call. 您的参数将需要在单个params调用中采用嵌套属性。 If that's confusing, check out this SO question and see in the answers how to nest those attributes. 如果这令人困惑,请查看此SO问题,并在答案中查看如何嵌套这些属性。

Make sure your form has ...fields_for :guests , too, as your Invite class has_many :guests 确保您的表单也具有...fields_for :guests ,因为您的Invite类has_many :guests

So, your invite_params implementation might end up looking something like this: 因此,您的Invitation_params实现可能最终看起来像这样:

def invite_params
  params.require(:invite).permit(:invite_code, :name, guests_attributes:[:name, :attendance_status, :starter, :main, :desert, :dietary_requirements])
end

Are you using Rails 4? 您在使用Rails 4吗? If so read up on strong parameters. 如果是这样,请阅读强参数。 Should look something like this in your invite controller. 在您的邀请控制器中应该看起来像这样。

private

  def invite_params
    params.require(:invite).permit(:invite_code, :name, guests_attributes:[:name, :attendance_status, :starter, :main, :desert, :dietary_requirements])
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM