trailblazer / reform

Form objects decoupled from models.
https://trailblazer.to/2.1/docs/reform.html
MIT License
2.49k stars 184 forks source link

Calling `#name` unexpectedly … #513

Closed antonysastre closed 4 years ago

antonysastre commented 4 years ago

Complete Description of Issue

My form is calling #name on it's model without (what I can see) reason?

class UserForm < Reform::Form
  model :user

  property :email
  property :first_name
  property :last_name
  property :password
  property :password_confirmation

  validates_uniqueness_of :email

  validates :first_name, :last_name, :email, presence: true
  validates :email, format: Devise.email_regexp
  validates :password, presence: true, if: -> { model.new_record? }
  validates :password, confirmation: true, length: Devise.password_length, allow_blank: true
end
def new
  def new
    @form = UserForm.new(User.new)
  end
end
class User < ActiveRecord::Base
  devise :database_authenticatable, :recoverable, :rememberable, :trackable
end

# Created by migration:
  create_table "users", id: :serial, force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "first_name"
    t.string "last_name"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

Expected behavior

A form view rendering - which works if I remove the Reform wrapper and just return the new user. I.e. @form = User.new works.

Actual behavior

I get a NoMethodError in the controller action (Not the view, no call to :name is made neither in the view nor in the model.

NoMethodError at /admin/users/new undefined method `name' for #<User:0x00007f9224de0d38>

System configuration

Reform version: 2.2.4

Started GET "/admin/users/new" for 127.0.0.1 at 2020-06-08 14:06:47 +0700
Processing by Admin::UsersController#new as HTML
  User Load (1.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  User Update (1.2ms)  UPDATE "users" SET "last_seen_at" = $1 WHERE "users"."id" = $2  [["last_seen_at", "2020-06-08 07:06:51.049370"], ["id", 1]]
Completed 500 Internal Server Error in 73ms (ActiveRecord: 12.4ms)

NoMethodError - undefined method `name' for #<User:0x00007f922310cf68>:
  app/controllers/admin/users_controller.rb:18:in `new'
antonysastre commented 4 years ago

Upgraded to 2.3.1 fixed the issue. Closing.