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

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

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

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

Ruby

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

Ruby on Rails

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

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

Q&A

解決済

1回答

2049閲覧

プロフィール編集(emailやpasswordなど)ボタンを押した後、登録内容が反映されない。&ユーザー詳細ページにリダイレクトしたい。

kousuke24

総合スコア29

Devise

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

Ruby

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

Ruby on Rails

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

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

0グッド

0クリップ

投稿2019/09/23 14:09

ruby on railsでアプリケーションを作成しています。deviseでログインと会員登録機能を作成し、ユーザー詳細ページと登録内容(名前・メール・パスワード等)を編集するページを追加しました。
イメージ説明

直面している問題が2つあります。
1,変更するのボタンを押した後、ユーザー詳細ページにリダイレクトしない
2,登録内容が反映されない
※またパスワードを入力しなくても変更できるようにコードをかいていますが、正しくかけているかはわかりません。

該当コード

ルーティング

Rails.application.routes.draw do get 'twitters/index' devise_for :users, controllers: { registrations: 'registrations' }, controllers: { omniauth_callbacks: 'users/omniauth_callbacks'} get 'tops/index' root to: 'tops#index' get '/users/:id', to: 'users#show', as: 'user' patch 'users/:id', to: 'users#update' #get '/users/edit/:id', to: 'users#edit', as: 'edit_user' resources :posts, only: %i(new create show destroy) do resources :photos, only: %i(create) resources :likes, only: %i(create destroy) end # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end

コントローラー

class RegistrationsController < Devise::RegistrationsController protected def update_resource(resource, params) resource.update_without_current_password(params) end def after_update_path_for(resource) user_path(resource) end end

モデル

class User < ApplicationRecord mount_uploader :image, ImageUploader has_many :posts, dependent: :destroy has_many :likes # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :omniauthable def self.fin_for_oauth(auth) user = User.where(uid: auth.uid, provider: auth.provider).first unless user user = User.create( uid: auth.uid, provider: auth.provider, email: User.dummy_email(auth), password: Devise.friendly_token[0, 20], image: auth.info.image, name: auth.info.name, nickname: auth.info.nickname, location: auth.info.location ) end user end validates :name, presence: true, length: { maximum: 30 } def update_without_current_password(params, *options) params.delete(:current_password) if params[:password].blank? && params[:password_confirmation].blank? params.delete(:password) params.delete(:password_confirmation) end result = update_attributes(params, *options) clean_up_passwords result end private def self.dummy_email(auth) "#{auth.uid}-#{auth.provider}@example.com" end end

ビュー(ユーザー詳細ページ)

<h3 class="text-center text-secondary mb-4">ユーザー情報</h3> <div class="row col-lg-6 offset-lg-3"> <div class="offset-lg-2 col-lg-4 text-center profile-image mb-3"> <%= form_for(@user) do |f| %> <% if @user.image? %> <%= image_tag @user.image.thumb.url, class: "round-img" %> <% else %> <%= image_tag "/assets/default.jpg", class: "round-img" %> <% end %> <% end %> </div> <div class="col-lg-6 text-center profile-text mb-3"> <h3><%= @user.name %></h3> <% if @user == current_user %> <p><%= @user.email %></p> <%= link_to "プロフィール編集", edit_user_registration_path, class: "btn btn-outline-dark common-btn edit-profile-btn" %> <% end %> </div> </div>

ビュー(登録内容編集ページ)

<h3 class="text-center text-secondary form-group">登録情報変更</h3> <div class="card-header"> <%= form_with scope: resource, as: resource_name, url: registration_path(resource_name), local: true, method: :patch do |f| %> <div class="ield form-group mt-3 text-center col-sm-6 offset-sm-3"> <p>プロフィール画像変更</p> <button type="button" class="btn btn-outline-secondary rounded-pill"> <%= f.file_field :image, accept: 'image/jpg,image/gif,image/png' %> </button> </div> <div class="ield form-group mt-3 text-center col-sm-6 offset-sm-3"> <%= f.label :name, "名前" %> <%= f.text_field :name, autofocus: true, class: "form-control" %> </div> <div class="ield form-group mt-3 text-center col-sm-6 offset-sm-3"> <%= f.label :email, "メールアドレス" %> <%= f.email_field :email, autofocus: true, class: "form-control" %> </div> <div class="ield form-group mt-3 text-center col-sm-6 offset-sm-3"> <%= f.label :password, "パスワード" %> <%= f.password_field :password, autofocus: "off", class: "form-control" %> </div> <div class="ield form-group mt-3 text-center col-sm-6 offset-sm-3"> <%= f.label :password_confirmation, "パスワード確認" %> <%= f.password_field :password_confirmation, autofocus: "off", class: "form-control" %> </div> <div class="actions form-group col-sm-6 offset-sm-3"> <%= f.submit "変更する", class: "btn btn-primary form-control" %> </div> <% end %> </div>

ご教授頂ければ幸いです。よろしくお願いします。

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

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

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

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

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

guest

回答1

0

自己解決

ルーティングのcontrollers: { omniauth_callbacks: 'users/omniauth_callbacks'}をコメントアウトしたら万事解決しました。コールバックされていたのか、処理がうまく噛み合わなかったのか、真相はわからないがとりあえず解決万歳。

投稿2019/09/24 05:00

kousuke24

総合スコア29

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問