2.10 Reinforce RCAV with /
Before we go on with the embedded Ruby, let’s reinforce RCAV a couple of times.
The target has a homepage at the root URL, /. In the old days, we would create a file in public/
called index.html
, but from here on we will connect any URL with routes.
Open the config/routes.rb
file and add a homepage route:
# config/routes.rb
Rails.application.routes.draw do
"/", { :controller => "application", :action => "homepage" })
get(
"/rock", { :controller => "application", :action => "play_rock" })
get(
end
{: mark_lines=“5” }
We are using get
to add the "/"
homepage route. :controller => "application"
signifies that we are using the app/controllers/application_controller.rb
file for our controller, and :action => "homepage"
is a yet to be defined action in the controller.
In the browser, pretend to be a user and navigate to the URL / in the address bar. RTEM.
The action 'play_rock' could not be found for ApplicationController
This is good! That means we defined the route correctly. If you still see a “No route matches” error, then double-check your route syntax in the get
method, save the changes, and get that error to go away before you proceed.
The new error occurs because we did not yet define the action! So we need to open app/controllers/application_controller.rb
and add:
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
false)
layout(
# Add your actions below this line
# ================================
def homepage
:template => "game_templates/rules.html.erb" })
render({ end
def play_rock
# write your code here
# redirect_to("https://www.wikipedia.org")
# render({ :html => "<h1>Hellow, world!</h1>".html_safe })
:template => "game_templates/user_rock.html.erb" })
render({ end
end
{: mark_lines=“9-11”}
Now create the view template file game_templates/rules.html.erb
, again in app/views/
where all our view templates live in organized subfolders. Once you create the file, add some HTML copy to it:
<!-- app/views/game_templates/rules.html.erb -->
<h1>Welcome to RPS</h1>
Now refresh /. There is no error and our HTML template is rendered. And we did not put any HTML in the public/
folder! You will almost always want some kind of dynamic behavior on every page. So our new workflow is always RCAV: Define a route, assign a controller, create an action in that controller, and view the result.
2.10.1 Completed Code
# config/routes.rb
Rails.application.routes.draw do
"/", { :controller => "application", :action => "homepage" })
get(
"/rock", { :controller => "application", :action => "play_rock" })
get(
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
false)
layout(
# Add your actions below this line
# ================================
def homepage
:template => "game_templates/rules.html.erb" })
render({ end
def play_rock
# write your code here
# redirect_to("https://www.wikipedia.org")
# render({ :html => "<h1>Hellow, world!</h1>".html_safe })
:template => "game_templates/user_rock.html.erb" })
render({ end
end
<!-- app/views/game_templates/rules.html.erb -->
<h1>Welcome to RPS</h1>