Undefined method `model_name` for reform object

696 views Asked by At

I keep on getting:

Undefined method `model_name` for #<DonationForm:0x007ff62ca75470>

I'm pretty sure I'm missing something. I've followed the installation instruction and everything. What could be the reason?

Model:

class Donation < ApplicationRecord
  belongs_to :campaign

  has_many :anotherthings
end

Controller:

def new
  @campaign = Campaign.new
  @donation = DonationForm.new(Donation.new)
end

View:

<%= simple_form_for [@campaign, @donation] do |f| %>
<% end %>

Form object:

# app/forms/donation_form.rb
class DonationForm < Reform::Form
  property :donation_amount
end
1

There are 1 answers

2
konung On

You contract needs to know which model it's connected to.

  1. Technically it can be anything - ActiveRecord model, PORO, OpenStruct, etc. But Reform contract needs to know where it's supposed to "direct" output, once it's done with validations, coercion, etc.
  2. If you don't specify the model, Reform will try to guess what it is, from the object passed to form.
  3. For ActiveRecord magic to work - you have to specify the model in the reform. This is only applicable to ActiveRecord, I think. If I remember correctly @apotonick mentioned in some discussion.

So what you are are missing is just one line

# app/forms/donation_form.rb
class DonationForm < Reform::Form
  model Donation  # Try adding this line. 
  property :donation_amount
end

P.S.:

This could be something unrelated - since you use Rails 5.1 . I haven't tried upgrading yet from 4.2.8

Also - our Gitter channel for Trailblazer project (including reform) is the best place to get help: https://gitter.im/trailblazer/chat