U-Yuri’s 健忘録

U-Yuri’s 備忘録

プログラミングを勉強しています。アウトプットに活用しているブログです。

【Rails】formのIDを含むactionの作成

<%= form_with model: @deck do |form| %>
  <h2>ポケモン</h2>
  <% @pokemons.each do |pokemon|%>
    <input type="checkbox" name="" value=""><%= pokemon.name %>
  <% end %>
  <h2>エネルギー</h2>
  <% @energys.each do |energy|%>
    <input type="checkbox" name="" value=""><%= energy.name %>
  <% end %>
  <%= form.submit "登録" %>
<% end %>

<%= form_with model: @deck do |form| %>を使用することで以下のようにaction="/pokemon/3"となる。
インスタンス変数の中身は@deck = Deck.find(params[:id])が入っている。

<form action="/pokemon/3" accept-charset="UTF-8" method="post"><input type="hidden" name="_method" value="patch" autocomplete="off" /><input type="hidden" name="authenticity_token" value="HsTbxp1QFiamJVYvdnQpYZpBB9c7mROqCzEFFvUevQb89V7FiuvQX3HZuE8mH1Nh1UxxvA8eH2r1fIRj58cQ_g" autocomplete="off" />
  <h2>ポケモン</h2>
      <input type="checkbox" name="" value="">ビッパ
      <input type="checkbox" name="" value="">ビーダル
  <h2>エネルギー</h2>
    <input type="checkbox" name="" value="">いちげきエネルギー
    <input type="checkbox" name="" value="">基本あくエネルギー
  <input type="submit" name="commit" value="登録" data-disable-with="登録" />
</form>
  </body>
</html>

参考:Action View フォームヘルパー - Railsガイド

【Rails】フォームの未入力を阻止

フォームが空で送信されるのを防ぐために、空項目ではフォームの送信ができないようにする方法。
required: trueを記述することで制約できる。

<div><%= form.label :商品名 %> <%= form.text_field :name, required: true %></div>

参考記事:RailsのSelectフォームで入力を必須(required)にする方法 – Tech Blog

【Rails】日本語化したら通貨表示がおかしくなった

devise-i18nrails-i18nを導入したところ、通貨表示が「¥30000」→「30000¥」になってしまった。
config/locals/ja.ymlで設定できる。

config/locals/の中にja.ymlを準備(rails-i18n/rails/locale/ja.yml at master · svenfuchs/rails-i18n · GitHubをコピペして貼り付け)

number:
    currency:
      format:
        delimiter: ","
        format: "%n%u"
        precision: 0
        separator: "."
        significant: false
        strip_insignificant_zeros: false
        unit: 円

この中のformat: "%n%u""%n%u"に、unit: 円unit: ¥に変更。

number:
    currency:
      format:
        delimiter: ","
        format: "%u%n"
        precision: 0
        separator: "."
        significant: false
        strip_insignificant_zeros: false
        unit: ¥

上記で設定したので、erb内の<%= number_to_currency(@sum, unit: "¥", precision: 0) %>%= number_to_currency(@sum) %>でよい。

以上。

【Rails】ログイン画面を日本語化

ログイン画面を日本語化するまで

gemをインストールする

まずはgemをインストールする。Gemfileに以下を記述。

gem "devise-i18n"
gem 'rails-i18n', '~> 7.0', '>= 7.0.8' #自分のrailsのバージョンを確認

rails-i18n | RubyGems.org | コミュニティのgemホスティングサービス

deviseのviewファイル作成

以下を実行しするとapp/view/deviseが作成され、中にdevaseに関係するviewファイルが作成される。

$ rails g devise:i18n:views


日本語化完了!!!!

【Rails】link_toの使い方

今回はdivで作成したbox自体にリンクをつけたい場合

<% @wants.each do |want| %>
          <%= link_to(want) do%>
            <div class="box">
              <div class="back_img">
                <%= image_tag want.image.variant(resize_to_limit: [200, 200])%>
                <p class="under_text">¥<%= want.money %></p>
              </div>
              <%= want.name %>
              <small><%= want.category.name %></small><br>
              <small><%= want.created_at %></small>
            </div>
        <% end %>

aタグを使う方法

<% @wants.each do |want| %>
        <a href="<%= want_url(want) %>">
          <div class="box">
            <div class="back_img">
              <%= image_tag want.image.variant(resize_to_limit: [200, 200])%>
              <p class="under_text">¥<%= want.money %></p>
            </div>
            <%= want.name %>
            <small><%= want.category.name %></small><br>
            <small><%= want.created_at %></small>
          </div>
        </a>

      <% end %>

参考URL:link_to (ActionView::Helpers::UrlHelper) - APIdock

link_to (ActionView::Helpers::UrlHelper) - APIdock

【Rails】timestampを日本時間に変更する

timestampの時間をUTC(協定時)から日本時間へ変更

config/application.rbconfig.time_zone = 'Tokyo'を追記する。

module Mudamuda
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.0

    # Configuration for the application, engines, and railties goes here.
    #
    # These settings can be overridden in specific environments using the files
    # in config/environments, which are processed later.
    #
    # config.time_zone = "Central Time (US & Canada)"
    # config.eager_load_paths << Rails.root.join("extras")
    config.time_zone = 'Tokyo'
  end
end