Home Changing the url on selecting a tab
Reply: 0

Changing the url on selecting a tab

user3349
1#
user3349 Published in June 19, 2018, 6:29 pm

I have various tab in my page which has url profile and when a tab is selected i want the url to change from profile to profile/*selected_tab_name*. The url is changed to what i need when the details is saved or after the form is submitted in the desired tab. I want the url to change once on pressing the tab. Can anyone help me with it!

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

routes.rb

resources :profile do
   collection do
     patch 'update_profile'
     patch 'change_password'
   end
 end

View code

<li class="nav-item m-tabs__item">
          <a class="nav-link m-tabs__link active" data-toggle="tab" href="#m_user_profile_tab_1" role="tab">
            <i class="flaticon-share m--hide"></i>
            Update Profile
          </a>
        </li>
        <li class="nav-item m-tabs__item">
          <a class="nav-link m-tabs__link" data-toggle="tab" href="#m_user_profile_tab_2" role="tab">
            Change Password
          </a>
        </li>
        <li class="nav-item m-tabs__item">
          <a class="nav-link m-tabs__link" data-toggle="tab" href="#m_user_profile_tab_3" role="tab">
            Settings
          </a>
        </li>

terminal log

Started PATCH "/profile/change_password" for 127.0.0.1 at 2018-05-15 10:20:55 +0530
Processing by ProfileController#change_password as HTML
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"KZDdXW/q89Iq2YBV0aobPi8hNXsn7sicGYgKs5/OOM0JF/1F8QOvHJdx9/S3st45VE1TrBWoJqmJIQage3QiyQ==", "user"=>{"current_password"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Save password"}
 User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
"============================="
  (0.2ms)  BEGIN
 SQL (0.3ms)  UPDATE `users` SET `encrypted_password` = '$2a$11$w14M.QgK7gBWtz.UyKamr.OdKn.O/gm9h/Vgb28uzaSZcH1SU7dSO', `updated_at` = '2018-05-15 04:50:55' WHERE `users`.`id` = 1
  (8.3ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 251ms (ActiveRecord: 9.1ms)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.360008 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO