2.11 Reinforce RCAV with /paper

Let’s reinforce the steps again with the /paper route, for when the user plays paper. First, define the route in config/routes.rb with a controller:

# config/routes.rb

Rails.application.routes.draw do
  
  get("/", { :controller => "application", :action => "homepage" })

  get("/rock", { :controller => "application", :action => "play_rock" })

  get("/paper", { :controller => "application", :action => "play_paper" })

end

{: mark_lines=“9” }

Again, we use the application_controller.rb. If we pretend we are a user now and go to /paper, we again get the “action not found” error. The helpful error message (RTEM!) tells us we need to define another method for the action in our controller called play_paper.

It’s the same flow, over and over again. Make a change, visit the route, RTEM, and find the next step.

Ok, then define the action in our controller:

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  layout(false)

  # Add your actions below this line
  # ================================
  
  def homepage
    render({ :template => "game_templates/rules.html.erb" })
  end

  def play_rock
    # write your code here
    
    # redirect_to("https://www.wikipedia.org")
    
    # render({ :html => "<h1>Hellow, world!</h1>".html_safe })

    render({ :template => "game_templates/user_rock.html.erb" })
  end

  def play_paper

    render({ :template => "game_templates/user_paper.html.erb" })
  end

end

{: mark_lines=“23-26”}

Refresh /paper, and we’ll get a new error message:

Missing template game_templates/user_paper.html.erb

If you keep getting the same error message, there are a few steps you can try:

  1. Carefully scan your code for a typo. Your syntax needs to be exact. Grammar matters!
  2. If you cannot figure out what your typo is and why an error message keeps coming up, then delete what you wrote and try to type it again from scratch.
  3. Explain the code line-by-line out loud to yourself or someone else.

Back to our new error, we RTEM, and that tells us to go and make the new game_templates/user_paper.html.erb file (in app/views/) that will be rendered to the user:

<!-- app/views/game_templates/user_paper.html.erb -->

<h2>
  We played paper!
</h2>

Refresh /paper. No error message? A successful RCAV!

2.11.1 Completed Code

# config/routes.rb

Rails.application.routes.draw do
  
  get("/", { :controller => "application", :action => "homepage" })

  get("/rock", { :controller => "application", :action => "play_rock" })

  get("/paper", { :controller => "application", :action => "play_paper" })

end
# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  layout(false)

  # Add your actions below this line
  # ================================
  
  def homepage
    render({ :template => "game_templates/rules.html.erb" })
  end

  def play_rock
    # write your code here
    
    # redirect_to("https://www.wikipedia.org")
    
    # render({ :html => "<h1>Hellow, world!</h1>".html_safe })

    render({ :template => "game_templates/user_rock.html.erb" })
  end

  def play_paper

    render({ :template => "game_templates/user_paper.html.erb" })
  end

end
<!-- app/views/game_templates/user_paper.html.erb -->

<h2>
  We played paper!
</h2>