Skip to content

Commit d906a87

Browse files
author
alex
committed
feat: Modify controllers/viws for nested resources
1 parent d1b56df commit d906a87

File tree

8 files changed

+35
-34
lines changed

8 files changed

+35
-34
lines changed

app/models/post.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Post < ApplicationRecord
2-
belongs_to :boards
2+
belongs_to :board
33
end

app/views/boards/index.html.erb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@
2525
<br>
2626

2727
<%= link_to 'New Board', new_board_path %>
28-
| <%= link_to 'Posts', posts_path %>

app/views/boards/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
</tbody>
2828
</table>
2929
<br>
30-
<%= link_to 'New Post', new_board_post_path(@board) |
30+
<%= link_to 'New Post', new_board_post_path(@board)%> |
3131
<%= link_to 'Edit', edit_board_path(@board) %> |
3232
<%= link_to 'Back', boards_path %>

app/views/posts/_form.html.erb

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
<%= form_with(model: post, local: true) do |form| %>
2-
<% if post.errors.any? %>
3-
<div id="error_explanation">
4-
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
1+
<% if @post.errors.any? %>
2+
<div id="error_explanation">
3+
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
54

6-
<ul>
7-
<% post.errors.full_messages.each do |message| %>
8-
<li><%= message %></li>
9-
<% end %>
10-
</ul>
11-
</div>
12-
<% end %>
13-
14-
<div class="field">
15-
<%= form.label :title %>
16-
<%= form.text_field :title, id: :post_title %>
5+
<ul>
6+
<% post.errors.full_messages.each do |message| %>
7+
<li><%= message %></li>
8+
<% end %>
9+
</ul>
1710
</div>
11+
<% end %>
1812

19-
<div class="field">
20-
<%= form.label :content %>
21-
<%= form.text_area :content, id: :post_content %>
22-
</div>
13+
<div class="field">
14+
<%= form.label :title %>
15+
<%= form.text_field :title, id: :post_title %>
16+
</div>
2317

24-
<div class="actions">
25-
<%= form.submit %>
26-
</div>
27-
<% end %>
18+
<div class="field">
19+
<%= form.label :content %>
20+
<%= form.text_area :content, id: :post_content %>
21+
</div>
22+
23+
<div class="actions">
24+
<%= form.submit %>
25+
</div>

app/views/posts/edit.html.erb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<h1>Editing Post</h1>
22

3-
<%= render 'form', post: @post %>
3+
<%= form_with(model: @post, local: true, :url => board_post_path(@board, @post)) do |f| %>
4+
<%= render 'form', form: f %>
5+
<% end %>
46

5-
<%= link_to 'Show', @post %> |
6-
<%= link_to 'Back', posts_path %>
7+
<%= link_to 'Show', board_post_path(@board, @post) %> |
8+
<%= link_to 'Back', board_posts_path(@board) %>

app/views/posts/new.html.erb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<h1>New Post</h1>
22

3-
<%= render 'form', post: @post %>
3+
<%= form_with(model: @post, local: true, :url => board_posts_path(@board)) do |f| %>
4+
<%= render 'form', form: f %>
5+
<% end %>
46

5-
<%= link_to 'Back', posts_path %>
7+
<%= link_to 'Back', board_posts_path(@board) %>

app/views/posts/show.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
<%= @post.content %>
1111
</p>
1212

13-
<%= link_to 'Edit', edit_post_path(@post) %> |
14-
<%= link_to 'Back', posts_path %>
13+
<%= link_to 'Edit', edit_board_post_path(@board, @post) %> |
14+
<%= link_to 'Back', board_posts_path(@board) %>

db/seeds.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
5.times do |i|
99
Board.create(name: "board ##{i+1}")
1010
2.times do |j|
11-
Post.create(title: "title for b#{i+1} p#{j+1}", content: "content for board ##{i+1} post ##{j+1}")
11+
Post.create(title: "title for b#{i+1} p#{j+1}", content: "content for board ##{i+1} post ##{j+1}", board_id: i+1)
1212
end
1313
end

0 commit comments

Comments
 (0)