質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2615閲覧

deviseを使ったパスワードなしユーザー編集機能が実装できない

75ks

総合スコア4

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2020/09/01 09:57

編集2020/09/04 03:33

deviseを使ったパスワード抜きのユーザー編集が実装できずに困っています。

編集ページでユーザー情報を編集した後に送信ボタンを押すと、
パスワードを入力してください
パスワードに半角英数字を使用してください
とエラーメッセージが表示されます。
編集ページでは、パスワードのフォームを表示していません。

registrations_controller.rbを編集してパスワードなしでも登録できるという記述を追加したのですが上手く機能していないのでしょうか?

詳細機能と、削除機能は問題なく実装できています。

現在のコードは下記の通りです。

routes

1Rails.application.routes.draw do 2 devise_for :users 3 root "posts#index" 4 resources :posts, only: [:new, :create, :show, :destroy, :edit, :update] do 5 resources :comments, only: :create 6 collection do 7 get "search" 8 end 9 end 10 resources :users, only: [:show, :destroy, :edit, :update] 11end

userscontroller

1class UsersController < ApplicationController 2 before_action :set_user, only: [:show, :destroy, :edit, :update] 3 before_action :correct_user, only: [:edit, :update] 4 5 def show 6 end 7 8 def destroy 9 if @user.destroy 10 flash[:notice] = "削除が完了しました" 11 redirect_to root_path 12 else 13 flash.now[:alert] = "削除に失敗しました" 14 render :show 15 end 16 end 17 18 def edit 19 end 20 21 def update 22 if @user.update(user_params) 23 flash[:notice] = "編集が完了しました" 24 redirect_to user_path(@user.id) 25 else 26 flash.now[:alert] = "編集に失敗しました" 27 render :edit 28 end 29 end 30 31 private 32 33 def user_params 34 params.fetch(:user, {}).permit(:nickname, :gender_id, :email) 35 end 36 37 def set_user 38 @user = User.find(params[:id]) 39 end 40 41 def correct_user 42 unless user_signed_in? && current_user.id == @user.id 43 redirect_to root_path 44 end 45 end 46end 47

registrationscontroller

1class Users::RegistrationsController < Devise::RegistrationsController 2 protected 3 4 def update_resource(resource, params) 5 resource.update_without_password(params) 6 end 7end

applicationcontroller

1class ApplicationController < ActionController::Base 2 before_action :configure_permitted_parameters, if: :devise_controller? 3 before_action :authenticate_user! 4 5 private 6 7 def configure_permitted_parameters 8 devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :gender_id]) 9 devise_parameter_sanitizer.permit(:account_update, keys: [:nickname, :gender_id]) 10 end 11end

edithtmlerb

1<%= render "shared/header"%> 2 3<div class="contents"> 4 <div class="container row"> 5 <%= form_with model: @user, url: user_path(@user.id), local: true do |f| %> 6 <h3 class="new-content">ユーザー編集</h3> 7 8 <%= render "shared/error_messages", model: f.object %> 9 10 <div class="field"> 11 <%= f.label :nickname, "ニックネーム" %><br> 12 <%= f.text_field :nickname, class:"user-field", placeholder:"ニックネームを入力してください", autofocus: true, maxlength: "10" %> 13 </div> 14 <div class="field"> 15 <%= f.label :gender_id, "性別" %><br> 16 <%= f.collection_select(:gender_id, Gender.all, :id, :name, {}, {class:"select-box"}) %> 17 </div> 18 <div class="field"> 19 <%= f.label :email, "メールアドレス" %><br> 20 <%= f.email_field :email, class:"user-field", placeholder:"PC・携帯どちらでも可" %> 21 </div> 22 23 <div class="btn-center"> 24 <%= f.submit "更新する", class:"user-btn btn" %> 25 </div> 26 27 <% if user_signed_in? && current_user.id == @user.id %> 28 <div class="delete-btn-container"> 29 <p> 30 ユーザーを削除 31 </p> 32 <%= link_to user_path(@user.id), method: :delete, data: {confirm: "削除しますか?"} do %> 33 <div class="delete-btn btn delete-user"> 34 <span class="delete-btn-text btn-text">削除</span> 35 </div> 36 <% end %> 37 </div> 38 <% end %> 39 40 41 <% end %> 42 </div> 43</div>

--------追記--------

userrb

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 extend ActiveHash::Associations::ActiveRecordExtensions 8 belongs_to_active_hash :gender 9 has_many :posts, dependent: :destroy 10 has_many :comments, dependent: :destroy 11 has_many :favorites, dependent: :destroy 12 has_many :favorite_posts, through: :favorites, source: :post 13 14 with_options presence: true do 15 validates :nickname 16 validates :gender_id 17 end 18 19 validates :email, uniqueness: true 20 21 validates :nickname, length: { maximum: 8, message: "は8文字以下にしてください" } 22 23 validates :gender_id, numericality: { other_than: 1, message: "を選択してください" } 24 25 half_width_alphanumeric = /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{6,100}+\z/i 26 validates :password, presence: true, format: { with: half_width_alphanumeric, message: "に半角英数字を使用してください" } 27end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2020/09/03 01:00

User の validation 部分も載せてください
75ks

2020/09/04 03:34

追加致しました。
guest

回答1

0

ベストアンサー

無条件で validates :password, presence: true だからですね
validates :password, presence: true, on: :create
にしてみてください。
ユーザ登録のときだけ必須 になります

投稿2020/09/04 03:37

winterboum

総合スコア23331

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

75ks

2020/09/04 06:36

教えていただいた記述で無事できました!ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問