Views in controllers
Building Rails apps entirely from Phlex starts from the controllers. Consider the following Rails controller that renders a list of blog posts for a blog.
class PostsController < ApplicationController
before_action do
@blog = current_user.blog
@posts = @blog.posts
end
# These are implicitly rendered by Rails if the `./app/views/posts/*.html.erb` file exists.
def index
render view: :index
end
def show
render view: :show
end
# ...
end
In Phlex, we’d have to render the views a little different by creating an instance of the Phlex component and passing it the data it craves.
class PostsController < ApplicationController
before_action do
@blog = current_user.blog
@posts = @blog.posts
end
def index
render Index.new(posts: @posts)
end
def show
render Show.new(post: @post)
end
# ...
end
🔓 Unlock content
Pre-order this course to unlock this video, source code, and content.
Pre-order video course for $379 $249
▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓ ▓▓▓ ▓▓ ▓▓▓ ▓▓▓▓▓▓ ▓▓▓▓ ▓▓▓ ▓▓▓▓▓▓ ▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓
class PostsController < ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓▓▓▓▓▓▓▓▓▓▓▓ do
▓▓▓▓▓ = ▓▓▓▓▓▓▓▓▓▓▓▓.▓▓▓▓
▓▓▓▓▓▓ = ▓▓▓▓▓.▓▓▓▓▓
end
class Index < ▓▓▓▓▓::▓▓▓▓
def ▓▓▓▓▓▓▓▓▓▓(▓▓▓▓▓:)
▓▓▓▓▓▓ = ▓▓▓▓▓
end
def ▓▓▓▓▓▓▓▓
▓▓ "Posts"
▓▓ do
▓▓▓▓▓▓.▓▓▓▓ do |▓▓▓▓|
▓▓ do
▓ href: ▓▓▓▓▓▓▓▓▓(▓▓▓▓), ▓▓▓▓.▓▓▓▓▓
end
end
end
end
end
def ▓▓▓▓▓
▓▓▓▓▓▓ ▓▓▓▓▓.▓▓▓(posts: ▓▓▓▓▓▓)
end
class Show < ▓▓▓▓▓::▓▓▓▓
def ▓▓▓▓▓▓▓▓▓▓(▓▓▓▓:)
▓▓▓▓▓ = ▓▓▓▓
end
def ▓▓▓▓▓▓▓▓
▓▓ ▓▓▓▓▓.▓▓▓▓▓
p ▓▓▓▓▓.▓▓▓▓
end
end
def ▓▓▓▓
▓▓▓▓▓▓ ▓▓▓▓.▓▓▓(post: ▓▓▓▓▓)
end
▓ ▓▓▓
end
▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓ ▓▓▓▓ ▓ ▓▓▓▓▓▓ ▓▓▓▓▓▓ ▓▓▓ ▓▓▓▓▓▓ ▓▓▓▓ ▓▓▓▓ ▓▓▓▓ ▓▓▓▓▓ ▓▓▓ ▓▓▓▓▓ ▓▓▓ ▓ ▓▓▓▓▓▓▓ ▓▓ ▓ ▓▓▓▓▓ ▓▓▓ ▓▓ ▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓ ▓▓▓ ▓▓ ▓▓▓▓ ▓▓ ▓▓ ▓▓▓ ▓▓▓▓ ▓▓▓ ▓▓▓▓ ▓▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓▓▓ ▓ ▓▓▓ ▓▓▓▓▓▓ ▓▓ ▓▓▓▓ ▓▓ ▓▓▓ ▓▓▓▓ ▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓ ▓▓▓▓ ▓▓▓ ▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓