Redirect to /notes
after POST /notes
requests
This commit modifies our NotesController#create
action’s behavior.
Instead of rendering a create.html.erb
template, respond by
redirecting the client to the notes#index
route.
Since we’re redirecting away from the notes#create
action, this commit
removes the notes/create.html.erb
template.
Collapse app/controllers/notes_controller.rb
Expand app/controllers/notes_controller.rb
app/controllers/notes_controller.rb
diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb
index 791c5b9..7534670 100644
--- a/app/controllers/notes_controller.rb
+++ b/app/controllers/notes_controller.rb
@@ -10,8 +10,6 @@ class NotesController < ApplicationController
def create
note = Note.create!(params.to_unsafe_h.slice(:content))
- render(locals: {
- note: note
- })
+ redirect_to notes_url
end
end
Collapse app/views/notes/create.html.erb
Expand app/views/notes/create.html.erb
app/views/notes/create.html.erb
diff --git a/app/views/notes/create.html.erb b/app/views/notes/create.html.erb
deleted file mode 100644
index d08c12b..0000000
--- a/app/views/notes/create.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= params.fetch(:content) %>
Collapse test/controllers/notes_controller_test.rb
Expand test/controllers/notes_controller_test.rb
test/controllers/notes_controller_test.rb
diff --git a/test/controllers/notes_controller_test.rb b/test/controllers/notes_controller_test.rb
index 0abf5c6..eebcb0a 100644
--- a/test/controllers/notes_controller_test.rb
+++ b/test/controllers/notes_controller_test.rb
@@ -6,7 +6,14 @@ class NotesControllerTest < ActionDispatch::IntegrationTest
post notes_path, params: { content: content }
- assert_response :success
assert_equal Note.pluck(:content), [content]
end
+
+ test "#create redirects to /notes" do
+ notes_attributes = { content: "Hello, World" }
+
+ post notes_path, params: notes_attributes
+
+ assert_redirected_to notes_url
+ end
end