LoginSignup
0
0

More than 1 year has passed since last update.

railsチュートリアル第五章 contactページ

Posted at

レイアウトのリンク

今度は'#'で代用していたリンクを書き換えてみましょう
RailsのERbテンプレートには素のHTMLを直接書くことも許されているので、次のようにリンクを直接記述することもできます。

<a href="/static_pages/about">About</a>

上の記法はRails流ではありません
boutページへのURLは /static_pages/about よりも /about の方がよいでしょう。
Railsでは次のようなコードでは名前付きルートを使うのが慣例となる。

<%= link_to "About", about_path %>

about_pathの定義を変えればabout_pathが使われているすべてのURLを変更できるため、柔軟性が高まります。

ページ名   URL   名前付きルート
Home    /      root_path
About    /about   about_path
Help     /help     help_path
Contact   /contact    contact_path
Sign up   /signup    signup_path
Log in    /login      login_path

Contactページ

コンタクトページのテスト
require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  test "should get home" do
    get static_pages_home_url
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"
  end

  test "should get help" do
    get static_pages_help_url
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
  end

  test "should get about" do
    get static_pages_about_url
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App"
  end

  test "should get contact" do
    get static_pages_contact_url
    assert_response :success
    assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
  end
end
Contactページのルートを追加する
Rails.application.routes.draw do
  root 'static_pages#home'
  get  'static_pages/home'
  get  'static_pages/help'
  get  'static_pages/about'
  get  'static_pages/contact'
end
Contactページ用のアクションを追加する
class StaticPagesController < ApplicationController
  .
  .
  .
  def contact
  end
end
Contactページのビューを追加する
<% provide(:title, 'Contact') %>
<h1>Contact</h1>
<p>
  Contact the Ruby on Rails Tutorial about the sample app at the
  <a href="https://railstutorial.jp/contact">contact page</a>.
</p>

これらの作業は前の章で演習としてcontactページを作っていた。

0
0
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
0
0