macOS Catalina 10.15.5 Rails 6.0.3
I'll write a Rails tutorial and a lot of it. The tutorial is compliant with the 6th edition because I want to get used to Github.
3.3.2 Red
I want to display the view from the URL/static_pages/about.
In what order do you check from $ rails t
?
① Create a URL in config/routes.rb
② Create about action in app/controllers/static_pages_controller.rb
③ Create about.html.erb
(view) in app/views/static_pages
test added
test/controllers/static_pages_controller_test.rb
test "should get about" do
get static_pages_about_url
assert_response :success
end
end
↓
$ rails t
NameError: undefined local variable or method `static_pages_about_url'
"Cannot find` static_pages_about_url'(URL) " ↓ ① Create a URL in routes.rb
config/routes.rb
get 'static_pages/about'
(When a GET request comes to the URL/static_pages/about, pass it to the about action of the StaticPages controller)
↓
$ rails t
AbstractController::ActionNotFound:
The action 'about' could not be found for StaticPagesController
"Static Pages controller has no about action" ↓ ② Create about action in static_pages_controller.rb
app/controllers/static_pages_controller.rb
def about
end
↓
$ rails t
ActionController::MissingExactTemplate:
StaticPagesController#about is missing a template for request formats: text/html
"There is no about
template in StaticPagesController
"
↓
③
Create about.html.erb
in app/views/static_pages
(Because the template = data in views
, for example, the about
action is associated with the view about.html.erb
.
This time it is a view related to static_pages
, so put it in the corresponding folder)
The file was created when I ran $ touch app/views/static_pages/about.html.erb
.
↓
$ rails t
The result is green.
Recommended Posts