Skip to content

Commit

Permalink
Only pass a block to render if one was actually given
Browse files Browse the repository at this point in the history
This call style/parameter is documented in e.g: https://guides.rubyonrails.org/layouts_and_rendering.html#rendering-an-action-s-template-from-another-controller

In b0cc8bc the code was changed from _only passing a block to render if one was originally given_ to _always passing a block to render regardless_. This is enough to break the call to render in some situations.

This change only passes a block to render if one was actually given to begin with.
  • Loading branch information
Burgestrand committed Nov 27, 2023
1 parent 1799dca commit b75432b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/phlex/rails/sgml/overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def render(*args, **kwargs, &block)
when Enumerable
return super unless renderable.is_a?(ActiveRecord::Relation)
else
@_context.target << @_view_context.render(*args, **kwargs) { capture(&block) }
block &&= -> { capture(&block) }
@_context.target << @_view_context.render(*args, **kwargs, &block)
end

nil
Expand Down
4 changes: 4 additions & 0 deletions spec/internal/app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ def index
end

def show; end

def preview
render Articles::PreviewView
end
end
7 changes: 7 additions & 0 deletions spec/internal/app/views/articles/preview_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Articles::PreviewView < Phlex::HTML
def template
render(template: "articles/show")
end
end
1 change: 1 addition & 0 deletions spec/internal/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

get "articles/index", to: "articles#index"
get "articles/show", to: "articles#show"
get "articles/preview", to: "articles#preview"
end
6 changes: 6 additions & 0 deletions spec/layout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@

expect(response.body).to eq %(<!DOCTYPE html><html><head><title>Article</title></head><body><main>\n<h1>Article</h1>\n</main></body></html>)
end

it "supports rendering templates" do
get "/articles/preview"

expect(response.body).to eq %(<!DOCTYPE html><html><head><title>Article</title></head><body><main>\n<h1>Article</h1>\n</main></body></html>)
end
end

0 comments on commit b75432b

Please sign in to comment.