Skip to content

Commit

Permalink
Fix issue with templates that don't have a virtual_path (#19)
Browse files Browse the repository at this point in the history
The layout patch needed to account for inline renders like
`render(plain: "hello")` or `send_data` that don't have a virtual path.

Without this fix, applications using props_template were throwing:

```
undefined method 'virtual_path' for an instance of ActionView::Template::Text
```
  • Loading branch information
jho406 authored May 31, 2024
1 parent 503e642 commit 61f582f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/props_template/layout_patch.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Props
module LayoutPatch
def render_template(view, template, layout_name, locals)
unless view.respond_to? :active_template_virtual_path
if !view.respond_to?(:active_template_virtual_path) && template.respond_to?(:virtual_path)
view.instance_eval <<~RUBY, __FILE__, __LINE__ + 1
def active_template_virtual_path; "#{template.virtual_path}";end
RUBY
Expand Down
10 changes: 10 additions & 0 deletions spec/layout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ def self.controller_path
expect(json.strip).to eql('{"success":"ok"}')
end
end

it "allows rendering of templates that do not have a virtual_path" do
view_path = File.join(File.dirname(__FILE__), "./fixtures")
controller = TestController.new
controller.prepend_view_path(view_path)

expect(
controller.render_to_string(plain: "some text")
).to eql "some text"
end
end

0 comments on commit 61f582f

Please sign in to comment.