Skip to content

Commit c6ad374

Browse files
committed
Update readme
1 parent 4b35a2e commit c6ad374

1 file changed

Lines changed: 9 additions & 15 deletions

File tree

README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ You probably want to use the same form for creating and editing resources. In Su
6161

6262
```ruby
6363
# app/views/posts/form.rb
64-
class Posts::Form < Components::Form
64+
class Views::Posts::Form < Components::Form
6565
def view_template
6666
Field(:title).text
6767
Field(:body).textarea(rows: 10)
@@ -77,7 +77,7 @@ Then render this in your views:
7777
```erb
7878
<!-- app/views/posts/new.html.erb -->
7979
<h1>New Post</h1>
80-
<%= render Posts::Form.new @post %>
80+
<%= render Views::Posts::Form.new @post %>
8181
```
8282

8383
Cool, but you're about to score a huge benefit from extracting forms into their own Ruby classes with automatic strong parameters.
@@ -92,21 +92,14 @@ class PostsController < ApplicationController
9292

9393
def create
9494
@post = Post.new
95-
if save Posts::Form.new(@post)
95+
if save Views::Posts::Form.new(@post)
9696
redirect_to @post, notice: 'Post created!'
9797
else
9898
render :new, status: :unprocessable_entity
9999
end
100100
end
101101

102-
def update
103-
@post = Post.find(params[:id])
104-
if save Posts::Form.new(@post)
105-
redirect_to @post, notice: 'Post updated!'
106-
else
107-
render :edit, status: :unprocessable_entity
108-
end
109-
end
102+
# ... other actions ...
110103
end
111104
```
112105

@@ -145,7 +138,7 @@ end
145138
Superform was built from the ground-up using Phlex components, which means you'll feel right at home using it with your existing Phlex views and components.
146139

147140
```ruby
148-
class Posts::Form < Components::Form
141+
class Views::Posts::Form < Components::Form
149142
def view_template
150143
div(class: "form-section") do
151144
h2 { "Post Details" }
@@ -438,11 +431,12 @@ Superform eliminates the need to manually define strong parameters. Just include
438431
```ruby
439432
class PostsController < ApplicationController
440433
include Superform::Rails::StrongParameters
434+
include Views::Posts
441435

442436
# Standard Rails CRUD with automatic strong parameters
443437
def create
444438
@post = Post.new
445-
if save Posts::Form.new(@post)
439+
if save Form.new(@post)
446440
redirect_to @post, notice: 'Post created successfully.'
447441
else
448442
render :new, status: :unprocessable_entity
@@ -451,7 +445,7 @@ class PostsController < ApplicationController
451445

452446
def update
453447
@post = Post.find(params[:id])
454-
if save Posts::Form.new(@post)
448+
if save! Form.new(@post)
455449
redirect_to @post, notice: 'Post updated successfully.'
456450
else
457451
render :edit, status: :unprocessable_entity
@@ -461,7 +455,7 @@ class PostsController < ApplicationController
461455
# For cases where you want to assign params without saving
462456
def preview
463457
@post = Post.new
464-
permit Posts::Form.new(@post) # Assigns params but doesn't save
458+
permit Form.new(@post) # Assigns params but doesn't save
465459
render :preview
466460
end
467461
end

0 commit comments

Comments
 (0)