sk

開発で得たこと

railsのdeviseまとめ

アカウント登録時に許可するパラメータの設定

  def sign_up_params
    params.require(:user).permit(:email, :password, :password_confirmation)
  end

リソースの更新時のストロングパラメータを変更する

app/controllers/users/registrations_controller.rb

  # protected
  ・・・・
  def account_update_params
    params.require(:user).permit(
      :email,
      :password,
      :password_confirmation,
      :current_password,
      profile_attributes: %i(id gender)
    )
  end
  ・・・

パスワードなしでリソースを更新する

app/controllers/users/registrations_controller.rb

  # protected
  ・・・・
  #
  # アカウント情報の更新時にはパスワードは不要
  #
  def update_resource(resource, params)
    resource.update_without_password(params)
  end
  ・・・

日本語化

config/locales/devise.ja.yml

ja:
  devise:
    confirmations:
      confirmed: "アカウントが確認されました。ログインしています。"
      send_instructions: "アカウントの確認方法を数分以内にメールでご連絡します。"
      send_paranoid_instructions: "ご登録のメールアドレスが保存されている場合、アカウントの確認方法をメールでご連絡します。"
    failure:
      already_authenticated: "既にログインしています。"
      inactive: "Your account is not activated yet."
      invalid: "Invalid email or password."
      locked: "アカウントがロックされています。"
      last_attempt: "あなたのアカウントがロックされる前に、もう1つの試みを持っています。"
      not_found_in_database: "メールアドレスまたはパスワードが無効です。"
      timeout: "一定時間が経過したため、再度ログインが必要です"
      unauthenticated: "ログインまたは登録が必要です。"
      unconfirmed: "本登録を行ってください。"
    mailer:
      confirmation_instructions:
        subject: "アカウントの登録方法"
      reset_password_instructions:
        subject: "パスワードの再設定"
      unlock_instructions:
        subject: "アカウントのロック解除"
    omniauth_callbacks:
      failure: "%{kind} から承認されませんでした。理由:%{reason}"
      success: "%{kind} から承認されました。"
    passwords:
      no_token: "このページにアクセスする事が出来ません。正しいURLでアクセスしている事を確認して下さい。"
      send_instructions: "パスワードのリセット方法を数分以内にメールでご連絡します。"
      send_paranoid_instructions: ""
      updated: "パスワードを変更しました。"
      updated_not_active: "パスワードを変更しました。"
    registrations:
      destroyed: "アカウントを削除しました。またのご利用をお待ちしております。"
      signed_up: "アカウント登録を受け付けました。"
      signed_up_but_inactive: "アカウントは登録されていますが、アクティブになっていないため利用できません。"
      signed_up_but_locked: "アカウントは登録されていますが、ロックされているため利用できません。"
      signed_up_but_unconfirmed: "確認メールを登録したメールアドレス宛に送信しました。リンクを開いてアカウントを有効にして下さい。"
      update_needs_confirmation: "アカウント情報が更新されました。更新の確認メールを新しいメールアドレス宛に送信しましたので、リンクを開いて更新を有効にして下さい。"
      updated: "アカウントが更新されました。"
    sessions:
      signed_in: "ログインしました。"
      signed_out: "ログアウトしました。"
    unlocks:
      send_instructions: "アカウントのロックを解除する方法を数分以内にメールでご連絡します。"
      send_paranoid_instructions: "アカウントが存在する場合、ロックを解除する方法をメールでご連絡します。"
      unlocked: "アカウントのロックが解除されました。ログインしています。"
  errors:
    messages:
      already_confirmed: "は既に登録済みです。ログインしてください"
      confirmation_period_expired: "%{period}以内に確認する必要がありますので、新しくリクエストしてください。"
      expired: "有効期限切れです。新規登録してください。"
      not_found: "は見つかりませんでした。"
      not_locked: "ロックされていません。"
      not_saved:
        one: "エラーにより、この %{resource} を保存できません:"
        other: "%{count} 個のエラーにより、この %{resource} を保存できません:"

プロフィールとは別でパスワードのみ編集する場合

  # PUT /resource
  def update
    if params[:user][:password]
      if resource.update_with_password(user_password_params)
        sign_in(resource, bypass: true)
        redirect_to users_settings_password_url, notice: t('devise.passwords.updated')
      else
        render 'users/settings/password'
      end
    else
      super
    end
  end
  ・・・・・
  #
  # パスワード更新時のパラメータ
  #
  def user_password_params
    params.require(:user).permit(
      :password,
      :password_confirmation,
      :current_password
    )
  end

参考

github.com

ProgateやRailsチュートリアル、プログラミングスクールを通い終えたが現場のコードはかけない、

一体どうやって書くの?と思っているエンジニアのみなさんのためのチュートリアルを公開しています。

チュートリアル