Skip to content

Commit b31fcbb

Browse files
committed
Update documentation
1 parent 065275f commit b31fcbb

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

README.md

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Superview
22

3-
Build Rails applications, from the ground up, using [Phlex](https://www.phlex.fun/) components, like this.
3+
Build Rails applications, from the ground up, using [Phlex](https://www.phlex.fun/) or [ViewComponent](https://viewcomponent.org/) components, like this.
44

55
```ruby
66
class PostsController < ApplicationController
@@ -17,6 +17,21 @@ class PostsController < ApplicationController
1717
end
1818
end
1919

20+
class Edit < ViewComponent::Base
21+
attr_accessor :post
22+
23+
def call
24+
<<~HTML
25+
<h1>Edit #{@post.title}</h1>
26+
<form action="<%= post_path(@post) %>" method="post">
27+
<input type="text" name="title" value="<%= @post.title %>">
28+
<textarea name="body"><%= @post.body %></textarea>
29+
<button type="submit">Save</button>
30+
</form>
31+
HTML
32+
end
33+
end
34+
2035
private
2136
def load_post
2237
@post = Post.find(params[:id])
@@ -41,11 +56,16 @@ If bundler is not being used to manage dependencies, install the gem by executin
4156

4257
## Usage
4358

44-
Install `phlex-rails` in your Rails application.
59+
Add `include Superview::Actions` to any controllers you'd like to render components as controller actions.
4560

46-
$ bin/rails generate phlex:install
61+
```ruby
62+
class PostsController < ApplicationController
63+
include Superview::Actions
64+
# Your code...
65+
end
66+
```
4767

48-
Then add `include Superview::Actions` to any controllers you'd like to render Phlex components.
68+
Then add classes to your controller that map to the actions you'd like to render. The `Show` class will render when the `PostsController#show` action is called and the `Edit` class will render when the `PostsController#edit` action is called.
4969

5070
```ruby
5171
class PostsController < ApplicationController
@@ -62,20 +82,37 @@ class PostsController < ApplicationController
6282
end
6383
end
6484

85+
class Edit < ViewComponent::Base
86+
attr_accessor :post
87+
88+
def call
89+
<<~HTML
90+
<h1>Edit #{@post.title}</h1>
91+
<form action="<%= post_path(@post) %>" method="post">
92+
<input type="text" name="title" value="<%= @post.title %>">
93+
<textarea name="body"><%= @post.body %></textarea>
94+
<button type="submit">Save</button>
95+
</form>
96+
HTML
97+
end
98+
end
99+
65100
private
66101
def load_post
67102
@post = Post.find(params[:id])
68103
end
69104
end
70105
```
71106

72-
The `Show` class will render when the `PostsController#show` action is called. To use along side other formats or render manually, you can define the `PostsController#show` as you'd expect:
107+
### Explicit rendering
108+
109+
You can explicitly render a component in a controller action method. In this example, we needed to render a the `Show` component in the `html` format and a JSON response in the `json` format.
73110

74111
```ruby
75112
class PostsController < ApplicationController
76113
include Superview::Actions
77114

78-
before_action :load_post
115+
# Your code...
79116

80117
class Show < ApplicationComponent
81118
attr_accessor :post
@@ -88,8 +125,10 @@ class PostsController < ApplicationController
88125

89126
def show
90127
respond_to do |format|
128+
# 👋 Renders the Show component
91129
format.html { render phlex }
92-
# These would also work...
130+
131+
# 👉 These would also work...
93132
# format.html { render Show.new.tap { _1.post = @post } }
94133
# format.html { render phlex Show.new }
95134
# format.html { render phlex Show }
@@ -98,10 +137,7 @@ class PostsController < ApplicationController
98137
end
99138
end
100139

101-
private
102-
def load_post
103-
@post = Post.find(params[:id])
104-
end
140+
# Your code...
105141
end
106142
```
107143

@@ -128,12 +164,8 @@ class PostsController < ApplicationController
128164
end
129165
end
130166

131-
private
132-
def load_post
133-
@post = Post.find(params[:id])
134-
end
167+
# Your code...
135168
end
136-
137169
```
138170

139171
### Extracting inline views into the `./app/views` folder

0 commit comments

Comments
 (0)