Summary
I want to build a rails monolith with Domain Objects that are easily portable as the app grows. I also want to start in the app/
directory instead of starting with a Rails Engine
because engines seem to be a lot of overhead for a feature that may not endure, or the feature might get moved to an HTTP
endpoint soon.
Progress
1. Scaffold
Running rails g scaffold post
generates the following structure in app/
(in addition to other files)
app/
controllers/
posts_controller.rb
models/
post.rb
views
posts/
2. Swap Directory order
Is it possible to config load paths in order to invert directories so that the following would work;
app/
post/
controllers/
post_controller.rb
models/
post.rb
views/
index, show, etc...
(I want to have all my MVC for Post
in the post/
directory in preparation to move post/
:
- down to
lib/
- to a gem
- into an engine
- to my micro service
- or maybe even to the trash because it's a terrible feature
3. Uninitialized constant PostsController
Currently, simply inverting files provides;
uninitialized constant PostsController
even with tinkering with variations of config.autoload_paths += %W( #{config.root}/app/post/* )
in application.rb
. @bbozo's suggestion below worked by explicitly including files rather than use '*' like so;
class Application < Rails::Application
config.autoload_paths += %W( #{config.root}/app/ )
config.autoload_paths += %W( #{config.root}/app/post/controllers )
config.autoload_paths += %W( #{config.root}/app/post/models )
4. ActionView::MissingTemplate
The next issue I'm having is ActionView::MissingTemplate
ActionView::MissingTemplate (Missing template posts/index, application/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/app/views"
):
.bundle/gems/actionview-4.2.0/lib/action_view/path_set.rb:46:in `find'
.bundle/gems/actionview-4.2.0/lib/action_view/lookup_context.rb:121:in `find'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/abstract_renderer.rb:18:in `find_template'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/template_renderer.rb:40:in `determine_template'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/template_renderer.rb:8:in `render'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/renderer.rb:42:in `render_template'
.bundle/gems/actionview-4.2.0/lib/action_view/renderer/renderer.rb:23:in `render'
.bundle/gems/actionview-4.2.0/lib/action_view/rendering.rb:100:in `_render_template'
.bundle/gems/actionpack-4.2.0/lib/action_controller/metal/streaming.rb:217:in `_render_temp
[cut out the rest]
From the rails guides I read about the file:
flag for absolutes so I got to the Index
when I added the following:
def index
@posts = Post.all
render file: 'app/post/views/posts/index'
end
The Index
rendered successfully but there was lots of re-configuration I had to do for this single endpoint. I'm trying to determine if I can manage this in the guts and it just works for Post#show, Post#edit, Post#create
and other Domain Objects such as Likes
5. Current status
Will I need to reconfigure application.rb
and render file:
for every endpoint?