具有has_one关系的嵌套表单出错 - “@practitioner [user_attributes]”不允许作为实例变量名称

时间:2015-12-18 16:39:06

标签: ruby-on-rails nested-forms

我正在尝试创建一个嵌套表单。 Practioner有一个User

class Practitioner < ActiveRecord::Base
  has_one :user
  accepts_nested_attributes_for :user

  private

  def practitioner_params
    params.require(:practitioner).permit(user_attributes: [:email, :password, :password_confirmation])
  end

end

我的表单看起来像这样

<%= form_for [:admin, @practitioner], url: spree.admin_practitioners_url, method: :post do |f| %>
  <%= f.fields_for :user do |user_fields| %>

    <%= user_fields.field_container :email, class: ['form-group'] do %>
      <%= user_fields.label :email, Spree.t(:email) %>
      <%= user_fields.email_field :email, :class => 'form-control' %>
      <%= error_message_on :user, :email %>
    <% end %>

    <%= user_fields.field_container :password, class: ['form-group'] do %>
      <%= user_fields.label :password, Spree.t(:password) %>
      <%= user_fields.password_field :password, :class => 'form-control' %>
      <%= error_message_on :user,:password %>
    <% end %>

    <%= user_fields.field_container :password, class: ['form-group'] do %>
      <%= user_fields.label :password_confirmation, Spree.t(:confirm_password) %>
      <%= user_fields.password_field :password_confirmation, :class => 'form-control' %>
      <%= error_message_on :user,:password_confirmation %>
    <% end %>

  <%end%>

  <%= f.field_container :name, class: ['form-group'] do %>
    <%= f.label :name, Spree.t(:name) %>
    <%= f.text_field :name, :class => 'form-control' %>
    <%= error_message_on :practitioner, :name %>
  <% end %>

  <%= f.field_container :location, class: ['form-group'] do %>
    <%= f.label :location, Spree.t(:location) %>
    <%= f.text_field :location, :class => 'form-control' %>
    <%= f.error_message_on :location %>
  <% end %>

<% end %>

我收到以下错误。我不知道为什么会出现这个错误。

    ActionView::Template::Error (`@practitioner[user_attributes]' is not allowed as an instance variable name):
        1: <%= f.fields_for :user do |user_fields| %>
        2:   <div data-hook="admin_user_form_fields" class="row">
        3:     <div class="col-md-6">
        4:       <%= user_fields.field_container :email, class: ['form-group'] do %>
        5:         <%= user_fields.label :email, Spree.t(:email) %>
        6:         <%= user_fields.email_field :email, :class => 'form-control' %>
        7:         <%= error_message_on :user, :email %>

如果我将<%= user_fields.field_container :email, class: ['form-group'] do %>更改为<%= f.field_container :email, class: ['form-group'] do %>,则代码可以正常运行。

修改

Practioner Controller

class PractitionersController < ResourceController

  def new
    @practitioner = Practitioner.new
    @practitioner.build_user
  end

  def create
    @practitioner = Practitioner.new(permitted_resource_params)
    if @practitioner.save
      collection
      render :index
    else
      render :new
    end
  end

1 个答案:

答案 0 :(得分:0)

使用 <%= f.error_message_on :field %>(而不是 <%= error_message_on :whatever, :field %>,适用于父对象。 在 :fields_for 内时,仅

<%= error_message_on nested_f.object, :field %>

似乎对我有用(nested_f 是嵌套对象的助手,所以在您的情况下是

<%= error_message_on user_fields.object, :email %>).