当我从同一页面中的一个标签移动到另一个标签时,如何更改URL地址?我想在routes.rb或控制器中更改代码?
routes.rb中:
Rails.application.routes.draw do
resources :dashboard, only: [:index]
resources :profile do
collection do
patch 'update_profile'
patch 'change_password'
end
end
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
root 'dashboard#index'
end
我当前的网址是localhost:3000/profile
,现在当我选择update_profile标签时,我需要网址为localhost:3000/profile/update_profile
而不呈现新网页
控制器代码:
class ProfileController < ApplicationController
before_action :set_user, only: %i[index update_profile]
def index
@address = if @user.address
@user.address
else
Address.new
end
end
def update_profile
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to profile_index_path, notice: 'Profile was successfully updated.' }
else
format.html { render :index }
end
end
end
def change_password
@user = current_user
if @user.update_with_password(user_password_params)
redirect_to root_path
else
render "index"
end
end
private
def set_user
@user = User.find(current_user.id)
end
def user_params
params.require(:user).permit(:name)
end
def address_params
params.require(:user).permit(address: %i[area state country])
end
def user_password_params
params.require(:user).permit(:password, :password_confirmation, :current_password)
end
end