学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails5.1 | 08 | Webアプリケーション開発 | トップページの見た目を変える

<<前  [TOP]  次>>


商品リストの表示は自動生成されたものです。。
編集するには「app/views/goods/index.html.erb」ファイルの記述を変更します。
まずはデフォルトの内容を見てみましょう。


【app/views/goods/index.html.erb】

<p id="notice"><%= notice %></p>

<h1>Goods</h1>

<table>
  <thead>
    <tr>
      <th>Goods</th>
      <th>Title</th>
      <th>Description</th>
      <th>Image url</th>
      <th>Price</th>
      <th>Date</th>
      <th>Maker</th>
      <th>Category</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @goods.each do |good| %>
      <tr>
        <td><%= good.goods_id %></td>
        <td><%= good.title %></td>
        <td><%= good.description %></td>
        <td><%= good.image_url %></td>
        <td><%= good.price %></td>
        <td><%= good.date %></td>
        <td><%= good.maker %></td>
        <td><%= good.category %></td>
        <td><%= link_to 'Show', good %></td>
        <td><%= link_to 'Edit', edit_good_path(good) %></td>
        <td><%= link_to 'Destroy', good, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Good', new_good_path %>

<%= と %>で囲まれている部分は、Rubyで書かれています。
このように、「.html.erb」ファイルはRubyのコードとHTMLのコードを一緒に記述することが出来ます。
まずは表示を日本語に直してみます。
【app/views/goods/index.html.erb】

<p id="notice"><%= notice %></p>

<h1>Railsはじめてマート</h1>

<table>
  <thead>
    <tr>
      <th>商品一覧</th>
      <th>商品名</th>
      <th>説明</th>
      <th>画像</th>
      <th>価格</th>
      <th>登録日</th>
      <th>メーカー</th>
      <th>カテゴリー</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @goods.each do |good| %>
      <tr>
        <td><%= good.goods_id %></td>
        <td><%= good.title %></td>
        <td><%= good.description %></td>
        <td><%= good.image_url %></td>
        <td><%= good.price %></td>
        <td><%= good.date %></td>
        <td><%= good.maker %></td>
        <td><%= good.category %></td>
        <td><%= link_to '詳細', good %></td>
        <td><%= link_to '編集', edit_good_path(good) %></td>
        <td><%= link_to '削除', good, method: :delete, data: { confirm: '本当に削除してもよろしいですか?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to '商品登録', new_good_path %>

このようになりました。

見た目が悪いですが、日本語表示にはなりました。
次は見た目を良くするためにスタイルシート「CSS」ファイルを編集してみます。
CSSファイルは「app/assets/stylesheets」フォルダにあります。
今回はアプリケーション内の全ページに適用される「scaffold.scss」というスタイルシートを編集します。
ちょっと長くなりましたが以下のように変えてみました。
【app/assets/stylesheets/scaffold.scss】

h1:first-letter {
	font-size: 2em;
	color: #7172ac;
}

h1 {
	position: relative;
	color: #333333;
	text-shadow: 0 0 2px white;
}

h1:before {
	content: "";
	position: absolute;
	background: #9de5ff;
	width: 50px;
	height: 50px;
	border-radius: 50%;
	top: 50%;
	/* border: dashed 1px white; */
	left: -15px;
	-moz-transform: translateY(-50%);
	-webkit-transform: translateY(-50%);
	-ms-transform: translateY(-50%);
	transform: translateY(-50%);
	z-index: -1;
}

table {
  width: auto;
  border-spacing: 0;
  font-size:14px;
}

table th {
  color: #fff;
  padding: 8px 15px;
  background: #7172ac;
  background:-moz-linear-gradient(rgba(34,85,136,0.7), rgba(34,85,136,0.9) 50%);
  background:-webkit-gradient(linear, 100% 0%, 100% 50%, from(rgba(34,85,136,0.7)), to(rgba(34,85,136,0.9)));
  font-weight: bold;
  border-left:1px solid #258;
  border-top:1px solid #258;
  border-bottom:1px solid #258;
  line-height: 120%;
  text-align: center;
  text-shadow:0 -1px 0 rgba(34,85,136,0.9);
  box-shadow: 0px 1px 1px rgba(255,255,255,0.3) inset;
}

table th:first-child {
  border-radius: 5px 0 0 0;
}

table th:last-child {
  border-radius:0 5px 0 0;
  border-right:1px solid #258;
  box-shadow: 2px 2px 1px rgba(0,0,0,0.1),0px 1px 1px rgba(255,255,255,0.3) inset;
}

table tr td {
  padding: 8px 15px;
  border-bottom: 1px solid #84b2e0;
  border-left: 1px solid #84b2e0;
  text-align: center;
}

table tr td:last-child {
  border-right: 1px solid #84b2e0;
  box-shadow: 2px 2px 1px rgba(0,0,0,0.1);
}

table tr {
  background: #fff;
}

table tr:nth-child(2n+1) {
  background: #f1f6fc;
}

table tr:last-child td {
  box-shadow: 2px 2px 1px rgba(0,0,0,0.1);
}

table tr:last-child td:first-child {
  border-radius: 0 0 0 5px;
}

table tr:last-child td:last-child {
  border-radius: 0 0 5px 0;
}

table tr:hover {
  background: #fffafa;
  cursor:pointer;
}

.btn {
    position: relative;
    display: inline-block;
    font-weight: bold;
    padding: 12px 0 8px;
    text-decoration: none;
    color: #67c5ff;
    transition: .4s;
}

.btn:before{
   position: absolute;
   content: '';
   width: 100%;
   height: 4px;
   top:100%;
   left: 0;
   border-radius: 3px;
   background:#67c5ff;
   transition: .2s;
}

.btn:after{
   position: absolute;
   content: '';
   width: 100%;
   height: 4px;
   top:0;
   left: 0;
   border-radius: 3px;
   background:#67c5ff;
   transition: .2s;
}

.btn:hover:before {
    top: -webkit-calc(100% - 3px);
    top: calc(100% - 3px);
}

.btn:hover:after {
    top: 3px;
}

body {
  background-color: #fff;
  color: #333;
  margin: 33px;
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: 13px;
  line-height: 18px;
}

p, ol, ul, td {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: 13px;
  line-height: 18px;
}

pre {
  background-color: #eee;
  padding: 10px;
  font-size: 11px;
}

div {
  &.field, &.actions {
    margin-bottom: 10px;
  }
}

#notice {
  color: green;
}

.field_with_errors {
  padding: 2px;
  background-color: red;
  display: table;
}

#error_explanation {
  width: 450px;
  border: 2px solid red;
  padding: 7px 7px 0;
  margin-bottom: 20px;
  background-color: #f0f0f0;

  h2 {
    text-align: left;
    font-weight: bold;
    padding: 5px 5px 5px 15px;
    font-size: 12px;
    margin: -7px -7px 0;
    background-color: #c00;
    color: #fff;
  }

  ul li {
    font-size: 12px;
    list-style: square;
  }
}

label {
  display: block;
}



「index.html.erb」の「 link_to」に「:class」属性を指定しました。
【app/views/goods/index.html.erb】

<p id="notice"><%= notice %></p>

<h1>Railsはじめてマート</h1>

<table>
  <thead>
    <tr>
      <th>商品ID</th>
      <th>商品名</th>
      <th>説明</th>
      <th>画像URL</th>
      <th>価格</th>
      <th>登録日</th>
      <th>メーカー</th>
      <th>カテゴリー</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @goods.each do |good| %>
      <tr>
        <td><%= good.goods_id %></td>
        <td><%= good.title %></td>
        <td><%= good.description %></td>
        <td><%= good.image_url %></td>
        <td><%= good.price %></td>
        <td><%=h ((good.date).to_date).strftime('%Y年%m月%d日') %></td>
        <td><%= good.maker %></td>
        <td><%= good.category %></td>
        <td><%= link_to '詳細', good, class: 'btn' %></td>
        <td><%= link_to '編集', edit_good_path(good), class: 'btn' %></td>
        <td><%= link_to '削除', good, method: :delete, data: { confirm: '本当に削除してもよろしいですか?' }, class: 'btn' %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to '商品登録', new_good_path, class: 'btn' %>



この変更により、トップページの見た目がこのように変わりました。



各メソッドについて書いておきます。


【link_toメソッド】
<%= link_to '詳細', good, class: 'btn' %>
最初に渡されるパラメータは、リンクを表すテキストです。
2番目のactionパラメータは、リンクのターゲットを指定します。
クリックしたときにここで指定したパラメータにジャンプします。
3番目のclassパラメータは、生成されるリンクのHTML属性の設定に使用されます。
<%= link_to '削除', good, method: :delete, data: { confirm: '本当に削除してもよろしいですか?' }, class: 'btn' %>
その他には、confirmパラメータがあり、リンク先への移動前に指定した確認メッセージを表示するJavaScriptを生成できます。
削除リクエストを行う場合、「method:」を使って「:delete」パラメータを呼び出す必要があります。


【strftime()メソッド】
<%=h ((good.date).to_date).strftime('%Y年%m月%d日') %>
日付のフィールドを指定したフォーマット表示出来るメソッドです。
date型のデータを「2018年1月30日」というように表示できるようになります。
%Yは西暦を表す数、%mは月を表す数字(01-12)、%dは日(01-31)を表します。
「date」フィールドに入っているデータ型はString型なので「.to_date」メソッドでdate型に変換しています。
「date」フィールドに入力する日付のパターンは「20180130」「2018/01/30」「2018-01-30」のどれでも大丈夫です。


↓↓クリックして頂けると励みになります。


<<前  [TOP]  次>>