2

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
konung
  • 6,908
  • 6
  • 54
  • 79
RubyCat
  • 155
  • 11

1 Answers1

1

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

konung
  • 6,908
  • 6
  • 54
  • 79
  • @RubyCat, just wondering if I hit the spot, with my answer? If you it did, could you kindly accept it? – konung Jun 21 '17 at 15:38
  • I had the same error and specifying model in Referm::Form did not help unfortunately. Also Rails 5.1 so maybe thats the clue ... – Artur79 Nov 09 '18 at 11:00