Category Archives: Polymorphism

Wot, no controller?

While considering where I was going wrong with my Rails project, I figured that since I had the model and the views covered, I ought to give some consideration to the controller.

Then it occurred to me that the reason why no form elements were being shown on the new commission page was because I hadn’t created any! The standard scaffold controller action can be a little basic with show and edit completely blank.

But the ‘new’ controller does create an object and this is where the form elements com from. And in earlier tests, I had seen that I could use a build method to create the nested attributes. This should be easy.

Surely all my controller needs is:

 def new
   @commission = Commission.new
   @commission.build_activity
 end

So why does that give me the following error:

undefined method `build_activity' for #<Commission:0x007f9e10be7020> Did you mean? build_commission_type

I can change the controller as suggested,

 def new
   @commission = Commission.new
   @commission.build_commission_type
 end

And although the page loads, there are no form elements displayed. It’s wrong anyway, so I’m not bothered about that.

Perhaps the problem boils down to using a word that that plurals with ‘ies’ and that needs to be reflected in the appropriate places: sometimes singular sometimes, plural, other times pluralised. Sometimes errors, sometimes nothing.

This is the Rails way: everything’s a battle. Every step of the way.

It couldn’t last

It didn’t take long for my development with polymorphic associations and forms to founder.

The very next stage after my last post on the matter, in fact. And I now have a second reference source that, while providing an additional nudge, isn’t enough to see me through the current block.

To be able to nest the activity section on the commission form, we need to tell the commission model to accept activity fields,

accepts_nested_attributes_for :activities

(I originally hd this as :activity after the model name, but it gave a missing model error; then my initial hunch was probably that accepts_nested_attributes_for was no longer supported in Rails 5; these things rarely work for very long).

A quick search on the topic of continued rails support for accepts_nested_attributes_for took me to https://rubyplus.com/articles/3681-Complex-Forms-in-Rails-5, which is a nicely written page which only backs up my original source. Curiously, however, it includes a step that shows successful data posts before they’ve been added to the form.

Then we simply add the following to the commission form,

 <% f.fields_for :activities do |act| %>
 Title: <%= act.text_field :title %>
 Details: %= act.textarea :details %>
Number of slots: <%= act.number_field :number_of_slots %>
 Min slot separation: <%= act.number_field :min_slot_separation %>
 <% end %>

Then nothing. No errors, log messages or anything. Nothing. Nothing ever works the way it’s described and a heavy price is to be paid for every advance.