現在、railsにてdeviseを用いてinstagram風のアプリを作成しているのですが、
パスワード変更ページに遷移するリンクを押すとなぜか、サインインページにリダイレクトされてしまいます。
何卒、よろしくお願いいたします。
解決したい課題
・ユーザー編集ページに設置したパスワード変更ページに遷移するリンクをクリックした際に、パスワード変更ページに遷移させたい。
rails routes
new_user_password GET /users/password/new(.:format) users/passwords#new edit_user_password GET /users/password/edit(.:format) users/passwords#edit user_password PATCH /users/password(.:format) users/passwords#update PUT /users/password(.:format) users/passwords#update POST /users/password(.:format) users/passwords#create cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit
app/views/devise/registrations/edit.htmo.erb
<% provide(:title, @user.username) %> <h1>Update your profile</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= devise_error_messages! %> <%= f.label :fullname %><br /> <%= f.text_field :fullname, autofocus: true, class: 'form-control' %> <%= f.label :username %><br /> <%= f.text_field :username, autofocus: true, class: 'form-control' %> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true, class: 'form-control' %> <%= f.submit "Update", class: "btn btn-primary" %> <% end %> <%= link_to "Password change", edit_user_password_path(@user) %> <div class="gravatar_edit"> <%= gravatar_for @user %> <a href="https://gravatar.com/emails" target="_blank">change</a> </div> </div> </div>
app/views/devise/passwords/edit.htmo.erb
<h2>Change your password</h2> <%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %> <%= render "devise/shared/error_messages", resource: resource %> <%= f.hidden_field :reset_password_token %> <div class="field"> <%= f.label :password, "New password" %><br /> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %> characters minimum)</em><br /> <% end %> <%= f.password_field :password, autofocus: true, autocomplete: "new-password" %> </div> <div class="field"> <%= f.label :password_confirmation, "Confirm new password" %><br /> <%= f.password_field :password_confirmation, autocomplete: "new-password" %> </div> <div class="actions"> <%= f.submit "Change my password" %> </div> <% end %> <%= render "devise/shared/links" %>
config/routes.rb
Rails.application.routes.draw do root 'pages#top' devise_for :users, controllers: { registrations: 'users/registrations', omniauth_callbacks: 'users/omniauth_callbacks', passwords: 'users/passwords'} resources :users do member do get :following, :followers end end resources :users, only: [:show, :index, :destroy] resources :microposts, only: [:create, :destroy] resources :relationships, only: [:create, :destroy] get '/notification', to:'pages#notification' get '/post', to:'pages#post' end
app/controllers/users/passwords_controller.rb
class Users::PasswordsController < Devise::PasswordsController def edit binding.pry super binding.pry redirect_to edit_user_password_path(@user) #redirect_to edit_user_password_path binding.pry end def update super end end
app/controllers/users_controller.rb
class UsersController < ApplicationController before_action :authenticate_user!, only: [:show, :destroy, :index, :following, :followers] before_action :set_user, only: [:show] before_action :admin_user, only: [:destroy] before_action :current_user, only: [:index] def index @users = User.paginate(page: params[:page]) end def show @user = current_user #binding.pry @microposts = @user.microposts.paginate(page: params[:page]) end def destroy User.find(params[:id]).destroy flash[:success] = "User deleted" redirect_to users_url end #def update #binding.pry #if @user.update(user_params) #redirect_to user_path(current_user) #else #redirect_to edit_user_registration_path(current_user) #end #end private def set_user @user = User.find(params[:id]) end def user_params params.fetch(:user, {}).permit(:username) end # 管理者かどうか確認 def admin_user redirect_to(root_url) unless current_user.admin? end # 正しいユーザーかどうか確認 def correct_user @user = User.find(params[:id]) redirect_to(root_url) unless current_user?(@user) end end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/12 09:29