Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

【Rails】parsley-rails導入メモ

More than 5 years have passed since last update.

railsでparsley-railsを導入してバリデーション設定をしたときのメモ
parsley-rails:github / ドキュメント

Gemfile

Gemfileにparsley-railsを追加する。

gem 'parsley-rails'

bundle installしてparsley-railsをインストール

$ bundle install
app/assets/javascript/application.js

application.jsに以下を追記
エラー文を日本語表記にしたいのでparsley.i18n.ja.jsも読み込む

//= require parsley
//= require parsley.i18n.ja
app/assets/stylesheet/application.css

デフォルトのCSSを利用するため、application.cssに以下を追記

*= require parsley
app/views/test/new.html.slim

viewの入力フォームのところを修正

= form_for :user, url: {action: :create},
  html: {'data-parsley-validate' => '', 'parsley-use-html5-constraints' => false} do |f|
  .row
    .col
      label.must 名前
      label.must (必須)
    .col
      = f.text_field :name, placeholder: "花咲 太郎",
        'data-parsley-trigger' => "change", 'required' => '', autofocus: true
  .row
    .col.title
      label 性別
    .col
      p
        = f.radio_button :gender, "Male", {'required' => ''}
        | Male
        = f.radio_button :gender, "Female"
        | Female

  button 登録

HTML5を使っているとform_forのところで自動的にnovalidateが入ってしまうので、
'parsley-use-html5-constraints' => falseで無効化させています。

s-mori
readyfor
想いをつなぎ、叶える未来を、つくる READYFORのOrganizationです
https://tech.readyfor.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away