Skip to content

Commit 91d813f

Browse files
committed
Nest articles under categories
1 parent 45fee15 commit 91d813f

File tree

6 files changed

+16
-8
lines changed

6 files changed

+16
-8
lines changed

app/controllers/articles_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ def create
1919
@article = Article.new(article_params)
2020

2121
if @article.save
22-
redirect_to @article, notice: 'Article was successfully created.'
22+
redirect_to [@article.category, @article],
23+
notice: 'Article was successfully created.'
2324
else
2425
render :new
2526
end
2627
end
2728

2829
def update
2930
if @article.update(article_params)
30-
redirect_to @article, notice: 'Article was successfully updated.'
31+
redirect_to [@article.category, @article],
32+
notice: 'Article was successfully updated.'
3133
else
3234
render :edit
3335
end

app/views/articles/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</ul>
77
<% end %>
88

9-
<%= form_for article do |f| %>
9+
<%= form_for [article.category, article] do |f| %>
1010
<p><%= f.text_field :title, placeholder: "Title" %></p>
1111

1212
<p><%= f.text_area :content, placeholder: "Content" %></p>

app/views/articles/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<% @articles.each do |article| %>
44
<article>
55
<h3>
6-
<%= link_to article.title, article %>
6+
<%= link_to article.title, [article.category, article] %>
77
</h3>
88

99
<p>

app/views/articles/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<h3>
33
<%= @article.title %>
44
<small>
5-
<%= link_to "Edit", [:edit, @article] %>
5+
<%= link_to "Edit", [:edit, @article.category, @article] %>
66
</small>
77
</h3>
88

config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
root to: "articles#index"
44

55
resources :articles
6+
7+
resources :categories do
8+
resources :articles
9+
end
610
end

spec/controllers/articles_controller_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
RSpec.describe ArticlesController do
44
describe "#show" do
5+
let(:category) { FactoryBot.create(:category) }
6+
57
before do
6-
FactoryBot.create(:article, permalink: "my-permalink")
8+
FactoryBot.create(:article, permalink: "my-permalink", category: category)
79
end
810

911
it "renders successfully" do
10-
get :show, params: { id: "my-permalink" }
12+
get :show, params: { id: "my-permalink", category_id: category }
1113

1214
expect(response.status).to eql(200)
1315
end
1416

1517
context "without correct permalink" do
1618
subject(:perform_request) {
17-
get :show, params: { id: "wrong-permalink" }
19+
get :show, params: { id: "wrong-permalink", category_id: category }
1820
}
1921

2022
it "raises error" do

0 commit comments

Comments
 (0)