Rails 带有 reform-rails 的表单对象,其集合无法正常工作或验证

Rails form object with reform-rails with collections not working or validating

我正在使用 reform-rails gem 以便在我的 rails 项目中使用表单对象。

我意识到表单对象对于我在下面使用的示例代码来说可能有点矫枉过正,但它仅用于演示目的。

在我正在创建 user 的表单中,与该用户记录关联的是两个 user_emails

# models/user.rb
class User < ApplicationRecord
  has_many :user_emails
end

# models/user_email.rb
class UserEmail < ApplicationRecord
  belongs_to :user
end

请注意,我没有在 User 模型中使用 accepts_nested_attributes_for :user_emails。在我看来,表单对象的要点之一是它可以帮助您摆脱使用 accepts_nested_attributes_for,所以这就是为什么我试图在没有它的情况下这样做。我从 this video 那里得到了关于重构胖模型的想法。我让 link 指向视频中关于表单对象的部分,他表达了他有多不喜欢 accepts_nested_attributes_for

然后我继续创建我的 user_form:

# app/forms/user_form.rb
class UserForm < Reform::Form
  property :name
  validates :name, presence: true

  collection :user_emails do
    property :email_text
    validates :email_text, presence: true
  end
end

因此 user_form 对象包装了一条 user 记录,然后包装了与该 user 记录关联的几条 user_email 记录。在 useruser_email 记录上有 表单级验证 此表单包装:

如果表单有效:那么它应该创建一个 user 记录,然后是几个关联的 user_email 记录。如果表单无效:那么它应该重新呈现带有错误消息的表单。

我将展示到目前为止控制器中的内容。为简洁起见:仅显示 new 操作和 create 操作:

# app/controllers/users_controller.rb
class UsersController < ApplicationController

  def new
    user = User.new
    user.user_emails.build
    user.user_emails.build
    @user_form = UserForm.new(user)
  end

  def create
    @user_form = UserForm.new(User.new(user_params))
    if @user_form.valid?
      @user_form.save
      redirect_to users_path, notice: 'User was successfully created.'
    else
      render :new
    end
  end

  private
    def user_params
      params.require(:user).permit(:name, user_emails_attributes: [:_destroy, :id, :email_text])
    end
end

最后:表格本身:

# app/views/users/_form.html.erb
<h1>New User</h1>
<%= render 'form', user_form: @user_form %>
<%= link_to 'Back', users_path %>

# app/views/users/_form.html.erb
<%= form_for(user_form, url: users_path) do |f| %>
  <% if user_form.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(user_form.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% user_form.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>
  <% f.fields_for :user_emails do |email_form| %>
    <div class="field">
      <%= email_form.label :email_text %>
      <%= email_form.text_field :email_text %>
    </div>
  <% end  %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

作为测试:这是带有输入值的表单:

现在我继续提交。应该发生的是验证错误,因为第二封电子邮件的值必须存在。但是,这里提交时是日志:

Parameters: {"utf8"=>"✓", "authenticity_token"=>”123abc==", "user"=>{"name"=>"neil", "user_emails_attributes"=>{"0"=>{"email_text"=>"email_test1"}, "1"=>{"email_text"=>""}}}, "commit"=>"Create User"}

ActiveModel::UnknownAttributeError (unknown attribute 'user_emails_attributes' for User.):

所以我的表单对象有问题。

如何让这个表单对象工作?是否可以使用 reform_rails 并使此表单对象工作 而无需 使用 accepts_nested_attributes?最终:我只是想让表单对象工作。

除了 reform-rails 文档之外,我已经探索了一些资源:

我第一次尝试创建表单对象也是使用 virtus gem, but I could not seem to get that one working either. I did post a 实现的。

完整答案:

型号:

# app/models/user.rb
class User < ApplicationRecord
  has_many :user_emails
end

# app/models/user_email.rb
class UserEmail < ApplicationRecord
  belongs_to :user
end

表单对象:

# app/forms/user_form.rb
# if using the latest version of reform (2.2.4): you can now call validates on property 
class UserForm < Reform::Form
  property :name, validates: {presence: true}

  collection :user_emails do
    property :email_text, validates: {presence: true}
  end
end

控制器:

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  before_action :user_form, only: [:new, :create]

  def new 
  end

  # validate method actually comes from reform this will persist your params to the Class objects
  # you added to the UserForm object. 
  # this will also return a boolean true or false based on if your UserForm is valid. 
  # you can pass either params[:user][:user_emails] or params[:user][user_email_attributes]. 
  # Reform is smart enough to pick up on both.
  # I'm not sure you need to use strong parameters but you can. 

  def create    
    if @user_form.validate(user_params)
      @user_form.save
      redirect_to users_path, notice: 'User was successfully created.'
    else
      render :new
    end
  end

  private

  # call this method in a hook so you don't have to repeat
  def user_form
    user = User.new(user_emails: [UserEmail.new, UserEmail.new])
    @user_form ||= UserForm.new(user)
  end 

  # no need to add :id in user_emails_attributes
  def user_params
    params.require(:user).permit(:name, user_emails_attributes: [:_destroy, :email_text])
   end
 end

表格:

# app/views/users/new.html.erb
<h1>New User</h1>
<%= render 'form', user_form: @user_form %>
<%= link_to 'Back', users_path %>

#app/views/users/_form.html.erb
<%= form_for(user_form, url: users_path) do |f| %>
  <% if user_form.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(user_form.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% user_form.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>
  <%= f.fields_for :user_emails do |email_form| %>
    <div class="field">
      <%= email_form.label :email_text %>
      <%= email_form.text_field :email_text %>
    </div>
  <% end  %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我终于成功了!!!

首先,我无法在 Rails 5 上保存集合。我创建了一个 4.2.6,它可以正常工作。我建议你在 Reform gem.

的 github 存储库页面上创建一个问题

所以,这是工作代码:

models/user.rb

class User < ActiveRecord::Base
  has_many :user_emails
end

models/user_email.rb

class UserEmail < ActiveRecord::Base
  belongs_to :user
end

user_form.rb

class UserForm < Reform::Form

  property :name
  validates :name, presence: true

  collection :user_emails, populate_if_empty: UserEmail do
    property :email_text
    validates :email_text, presence: true
  end
end

populate_if_empty 在进行验证时很重要。

控制器创建方法:

def create
  @user_form = UserForm.new(User.new)
  if @user_form.validate(user_params)
    @user_form.save
    redirect_to users_path, notice: 'User was successfully created.'
  else
    render :new
  end
end

这将 validates 您的用户模型以及任何嵌套关联。

给你!干燥模型、模型和关联的验证和保存。

希望对您有所帮助!