Sha256: 6df59c9a79767561322e09c56b10c4b9c0ed8ccf1c308c0ad477d2e8a8c05945

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Pages
	class Rails::RenderingViews < ApplicationPage
		def template
			render Layout.new(title: "Getting started with Rails") do
				render Markdown.new <<~MD
					# Rendering Phlex views in Rails

					You can render a `Phlex::View` from your Rails controller actions as well as other views, and even from ActionView / ViewComponent templates.

					Instead of implicitly rendering an ERB template with automatic access to all your controller instance variables, you need to explicitly render Phlex views from your controller action methods.

					Doing this allows you to design views without implicit dependencies on controller instance variables, making them much easier to test and reuse and reason about.

					```ruby
					class ArticlesController < ApplicationController
						def index
							render Views::Articles::Index.new(
								articles: Article.all.load_async
							)
						end

						def show
							render Views::Articles::Show.new(
								article: Article.find(params[:id])
							)
						end
					end
					```
				MD
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phlex-0.4.0 docs/pages/rails/rendering_views.rb