LoginSignup
5
8

More than 5 years have passed since last update.

Ruby on Rails チュートリアル 第1章 - 演習&答え

Last updated at Posted at 2015-11-22

演習

  1. リスト1.8のhelloアクションを書き換え、「Hello, world!」の代わりに「hola, mundo!」と表示されるようにしてみましょう。課外作業: Railsの表示では「非ASCII文字」もサポートされています。スペイン語特有の逆さ感嘆符「¡」を含む「¡Hola, mundo!」を表示してみましょう (図1.19)20。
  2. リスト1.8のhelloアクションを複製して、第2のアクションgoodbyeを追加しましょう。このアクションは、「goodbye, world!」というテキストを表示します。リスト1.10のルーティングを編集して、ルートルーティングの割り当て先をhelloアクションからgoodbyeアクションに変更します (図1.20)。

出所)Ruby on Rails チュートリアル:実例を使って Rails を学ぼう

答え

1.
controllersを変更
hello_app/app/controllers/application_controllers.rbにて、下記を追加

application_controllers.rb
def hello
    render text: "¡Hola, mundo!"
end

2.
①controllersに追加
hello_app/app/controllers/application_controllers.rbにて、下記を追加

application_controllers.rb
def goodbye
render text: "goodbye, world!"
end

②routes.rbに追加と編集
hello_app/config/routes.rb にて、下記を追加

routes.rb
#root 'application#hello'
root 'application#goodbye'

終わり

5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8