Changing URL in the web page

删除回忆录丶 提交于 2020-01-25 11:15:05

问题


How could i change the URL address while i'm moving from one tab to another tab within the same page? Where i want to change the code either in the routes.rb or in controller?

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

My current url is localhost:3000/profile, now when i select update_profile tab i need the url to be localhost:3000/profile/update_profile without rendering the new page

controller code:

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

来源:https://stackoverflow.com/questions/50323880/changing-url-in-the-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!