Skip to content

Commit

Permalink
Use articles resource for routing
Browse files Browse the repository at this point in the history
Change the routes configuration to use resources :articles instead of
specifying every single route for the Article resource one by one.

* Replace per action Article routes with resource in routes config
* Update the index view to use the link_to helper for each Article
* Update the ArticlesControllerTest to use article_path

Completes Rails Getting Started Guide:
* 7 CRUDit Where CRUDit Is Due
  * 7.2 Resourceful Routing
  • Loading branch information
msducheminjr committed Dec 23, 2021
1 parent c2debae commit 0cc1414
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 6 deletions.
4 changes: 1 addition & 3 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
<ul>
<% @articles.each do |article| %>
<li>
<a href="/articles/<%= article.id %>">
<%= article.title %>
</a>
<%= link_to article.title, article %>
</li>
<% end %>
</ul>
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Rails.application.routes.draw do
root "articles#index"

get "/articles", to: "articles#index"
get "/articles/:id", to: "articles#show"
resources :articles
end
2 changes: 1 addition & 1 deletion test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setup
end

test "should get show" do
get "/articles/#{@article.id}"
get article_url(@article)
assert_response :success
assert_select 'h1', @article.title
assert_select 'p', @article.body
Expand Down

0 comments on commit 0cc1414

Please sign in to comment.