Undefined method `template_options_for` within an Engine
Closed this issue · 5 comments
I am using cells ~> 4.1
in a Rails engine that has no expectation of the base application to have cells installed. I have the following gems included as dependencies in my .gemspec
:
s.add_dependency "rails", ">= 4"
s.add_dependency "cells", "~> 4.1"
s.add_dependency "cells-rails", "~> 0.0.6"
s.add_dependency "cells-haml", "~> 0.0.10"
As of right now I have a relatively basic setup with one cell class, HelloWorldCell
, this is the source so far:
class HelloWorldCell < Cell::ViewModel
view_paths << "#{MyEngineName::Engine.root}/app/cells"
def show
render
end
end
In the corresponding view slot for this cell I have exactly one view show.haml
that has basic hall markup to simply say "Hello World". When I try to load this cell in a paired Rails app I consistently get the following error:
NoMethodError: undefined method `template_options_for' for #<HelloWorldCell:0x007f83c61d8d90>
Did you mean? template_for from #{PATH}
Is there some piece I'm missing from this to get the views to render when called when using cells in an engine?
Thank you!
Hi El, you need to include the respective cell format module into the cells.
class Hello < Cell::ViewModel
include Cell::Haml
Two things: please use trailblazer-cells
wherever you can to stay forward compatible (only a few things in the behavior have changed) and avoid Haml wherever you can, it's dead, use Slim.
Perfect, thank you very much!
I'm having this problem, too, just with ERB. I'm not including Cell::Erb
in my ViewModel since the README states this isn't necessary when using Rails. Also, I tried including it anyways and got another error: uninitialized constant Cell::Erb
(even though I have spec.add_dependency "cells-erb", "~> 0.1"
in my gemspec).
@ElliottAYoung Is the project you are working on public? I'm also trying to create an Engine as a gem that uses Cells and any guidance would be really helpful. It would be great, if you could point out resources you used to find out best practices etc.
EDIT: i fixed it, I needed to do the include Cell::Erb
but was missing a require "cell/erb"
@TheFlow0360 The project is public, you can access it here: https://github.com/ElliottAYoung/funfetti
I haven't done a whole lot with it yet / written sufficient documentation, so I do apologize for that. But you should be able to see all relevant examples for including cells in an engine in /app/cells/funfetti/cell.rb and see examples for how to call this in lib/confetti
@ElliottAYoung thanks a lot, I'll take a look!