BLAT

Taken from the back of the i newspaper in the UK which lists it as from ‘All day cafe’ by Stuart McKenzie (Murdoch books), but it appeals to me because I can easily make the aioli and pide and, if all goes well in the garden, will be able to source the lettuce and tomato.

The avocado is mashed with lemon juice and salt and pepper and spread on one half of toasted pide. Aioli  is spread on the other half, and in between is the cooked (in olive oil) bacon, lettuce and tomato.

IMG_20170415_124357685

A great lunch after a morning spent in the garden weeding.

Pide and BLAT

I have been cutting out the daily recipe from the i newspaper for some time now and recently there was one for a BLAT (bacon, lettuce, avocado and tomato) with pide bread and aioli.

Well, for me, making the bread is a no-brainer and I have made pide before. But since I have recently been making my own aioli, this seemed like too good an opportunity to miss.

IMG_20170415_091615695

Okay, so maybe they’re supposed to be round, but decided right at the end to make more, smaller breads and didn’t want to have to wait too long again to prove them.

The BLAT post to follow.

Saying goodbye to Rails

I give up.

Everywhere I look suggests that what I have should be enough for this to work. But it isn’t.

And since this is a fundamental feature of the project, and what with the still patchy cross-browser performance of JQuery auto-select, it’s time to abandon Rails and look for another framework.

So, I will be taking a pause while I get Django up and running. Rails is no more.

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.

3 sources, all slightly different, none work

Now on the case with http://apidock.com/rails/v4.2.1/ActionView/Helpers/FormHelper/fields_for (yes, I know, Rails 4, probably isn’t supported in 5) and mentions more possibilities that probably aren’t relevant, but don’t work anyway.

I mention it here because if (I’m not confident enough to say when) I get this working I will be needing to auto-populate some fields (am I mad!) and there might be value in the comments.

It’s another one of those times when it’s clear that I’m doing something wrong (other than actually attempting to do this) but I have no idea what and very little indication of where to start looking. For all the times that development can accelerate with Rails, there are these occasions when it grinds to a halt and you’re never actually sure how it got fixed.

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.

 

Rails polymorphic associations

The first in a likely series of posts regarding polymorphic associations in Rails that will illustrate my hitherto lack of understanding about how they work and my (hopeful) development to being able to use them effectively in my current project.

I’m going to need reminders and reference points when I get deeper into the project and start to make progress. I don’t necessarily expect this to be of use use to anyone else.

First off, I’m using¬†https://6ftdan.com/allyourdev/2015/02/10/rails-polymorphic-models/ as a reference; so far it’s working well for me.

Start with a new git branch in case things don’t work out.

$ git checkout -b polymorphic_activities

The activities table already includes activityable_type and activityable_id columns but I needed another migration to the table to do the half-baked ideas I had before/

The Commission model includes,

class Commission < ApplicationRecord
belongs_to :request
 belongs_to :commission_type
 has_many :activities, :as => :activityable, :dependent => :destroy

The Activity model includes

class Activity < ApplicationRecord
 validates :title, :presence => true, :uniqueness => true
 validates :activityable_id, :presence => true
 validates :activityable_type, :presence => true
 
 belongs_to :activityable, :polymorphic => true

And we can test this with the Rails console with something like,

2.3.3 :002 > commission = Commission.first
2.3.3 :005 > commission.activities.build(title: "Background layers", details: "do some prep layers", number_of_slots: 3, min_slot_separation: 3).save

And we get an entry in the activities table with activityable_type set to Commission and activityable_id set to the id of the commission. Looks promising.

The main point at this stage is that I can bin the activity_commission, activity_other, activity_teaching tables (with ActivityCommission as the activityable_type) and just use Commission, etc. I’m beginning to see how this might work for all the other activity-based relationships I need to model. No need to abandon the project just yet.