6

我正在使用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

请注意,我没有accepts_nested_attributes_for :user_emailsUser模型中使用。在我看来,表单对象的主要观点之一是它可以帮助您摆脱使用accepts_nested_attributes_for,所以这就是为什么我试图在没有它的情况下这样做。我从这个关于重构胖模型的视频中得到了这个想法。我有指向表单对象视频部分的链接,他表达了他多么不喜欢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_email该记录关联的user记录。此表单包装的记录和记录上有表单级验证:useruser_email

  • user#name必须有一个值
  • 每个都user_email#email_text必须有一个值

如果表单是有效的:那么它应该创建一个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,但我似乎也无法让那个对象工作。我也为该实现发布了一个stackoverflow问题。

4

2 回答 2

8

完整答案:

楷模:

# 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 %>
于 2017-03-21T23:15:42.567 回答
1

我终于搞定了!!!

首先,我无法将它保存在 Rails 5 上。我创建了一个 4.2.6,它适用于我们的盒子。我建议您在改革 gem 的 github 存储库页面上创建一个问题。

所以,这是工作代码:

模型/用户.rb

class User < ActiveRecord::Base
  has_many :user_emails
end

模型/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

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

你有它!干燥模型、验证和保存模型和关联。

我希望这有帮助!

于 2017-03-22T04:01:21.603 回答