I have a Boilerplate
model which has two descending models:
BoilerplateOriginal
BoilerplateCopy
While BoilerplateOriginal
s is sort of a template that admins create, BoilerplateCopy
s are copies of the originals that are free to edit by everyone, and they also have some more associated objects (e.g. BoilerplateCopy belongs_to: BoilerplateOriginal
, BoilerplateCopy belongs_to: Project
or BoilerplateCopy has_many: Findings
, all of which BoilerplateOriginal
doesn't).
So in my opinion, it makes sense to maintain two different model classes that share the same basic functionalities.
Because they also look quite the same, I want to use the same views for them. But under the hood, they are treated a bit different, so I also have two different controllers inheriting from a base controller.
Everything seems to work fine, except that form_for boilerplate, as: resource_instance_name
raises an exception undefined method
boilerplates_path', but only when called as
newaction, not when called as
edit` action.
Here's what I have done so far to make it work (and everything else seems to work fine):
# config/routes.rb
resources :boilerplate_originals
# app/models/boilerplate.rb
class Boilerplate < ActiveRecord::Base
def to_partial_path
'boilerplates/boilerplate'
end
end
# app/models/boilerplate_original.rb
class BoilerplateOriginal < Boilerplate
end
# app/controllers/boilerplates_controller.rb
class BoilerplatesController < InheritedResources::Base
load_and_authorize_resource
private
def boilerplate_params
params.require(:boilerplate).permit(:title)
end
end
# app/controllers/boilerplate_originals_controller.rb
class BoilerplateOriginalsController < BoilerplatesController
defaults instance_name: 'boilerplate'
end
# app/views/boilerplates/_form.html.slim
= form_for boilerplate, as: resource_instance_name
# ...
As pointed out, new/create
works flawlessly, but edit
doesn't. And I'm using InheritedResources, by the way.