I moved my render_listable helper into a presenter and now my render path is wrong, as the render method can not see the template.
How do I fix the path to the template path?
Error
ActionView::Template::Error (Missing partial search/_listing_car with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :vtt, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :mp3, :ogg, :m4a, :webm, :mp4, :otf, :ttf, :woff, :woff2, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: ):
> 3: <div class="col-md-12 vertical-right">
> 4: <% if listings && listings.size > 0 %>
> 5: <% listings.each do |listing_object| %>
> 6: <%= listing_object.render_listable %>
> 7: <hr/>
> 8: <% end %>
> 9:
>
app/presenters/listing_presenter.rb:60:in
render_listable' app/views/search/_searchresults.html.erb:6:in
block in _app_views_search__searchresults_html_erb___9192204423298974_70140171600340' app/views/search/_searchresults.html.erb:5:ineach' app/views/search/_searchresults.html.erb:5:in
_app_views_search__searchresults_html_erb___919234423298974_70140171600340' app/views/search/index.js.erb:3:in `_app_views_search_index_js_erb___2514407173266410_70140171518260'
My templates are in..
MyAppName/app/views/search/_listing_car.html.erb MyAppName/app/views/search/_listing_truck.html.erb
Presenter
class ListingPresenter < ApplicationPresenter
def render_listable
case listable_type
when "Car"
h.render partial: "search/listing_car", locals: { listing: self }
when "Truck"
h.render partial: "search/listing_truck", locals: { listing: self }
else
"NO TEMPLATE RENDERED"
end
end
end
Application Presenter
class ApplicationPresenter
delegate_missing_to :@object
def initialize(object)
@object = object
end
def h
ActionController::Base.helpers
end
def routes
Rails.application.routes.url_helpers
end
def self.present(obj, presenter_class = nil)
klass = presenter_class || presenter_name(obj).constantize
klass.new obj
end
def self.wrap_collection(collection)
collection.map { |obj| self.present(obj) }
end
private
def self.presenter_name(model)
if model.class == Listing
"#{model.listable_type}Presenter"
else
"#{model.class}Presenter"
end
end
end
Note:
I noticed that if I pass a viewcontext in, rather than use my ActionController::Base.helpers method (the h method), it works.
However, I would prefer to not have to pass the view_context in.
def render_listable(view_context)
case listable_type
when "Car"
view_context.render partial: "search/listing_car", locals: { listing: self }
when "Truck"
view_context.render partial: "search/listing_truck", locals: { listing: self }
else
"NO TEMPLATE RENDERED"
end
end
So when I call from the view..
listing.listable_type(self)
,
but I want
listing.listable_type