ruby-on-railsruby-on-rails-4.1reform

Unexpected naming of fields using Reform Form Object with Composition


ANSWER: The model calls were redundant. There should only be one and the last one in this example was the winner. I was misusing the Form Object DSL. :/

I've got a Reform Form object in a Rails 4.1 form that is structured like...

Form Object

class MyForm < Reform::Form
  include Composition

  model :user
  model :user_group

  property :name, on: :user_group
  property :email, on: :user
end

Controller

# ...
@form = MyForm.new(user: User.new, user_group: UserGroup.new)
# ... 

View

<%= form_for(@form) do |f| %>
<%= f.text_field(:name) %>
<%= f.email_field(:email) %>
<% end %>

Rendered HTML

<input type="text" name="user_group[name]" id="user_group_name">
<input type="email" name="user_group[email]" id="user_group_email">

My question is why are the fields seeming to ignore the model mappings and rendering them to the wrong model name? What am I doing incorrectly here?


Solution

  • You can call ::model only once! Once you call it with the correct model (or any name), the rendering will name the fields to whatever ::model you specify. The point about Reform is to hide internals about your model names!