我的页面中有各种标签,其中包含网址profile
,当选择标签时,我希望网址从profile
更改为profile/*selected_tab_name*
。保存详细信息或在所需选项卡中提交表单后,网址将更改为我需要的内容。我希望在按下标签时更改网址一次。任何人都可以帮助我!
控制器代码
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
查看代码
<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>
终端日志
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)
答案 0 :(得分:0)
下载Redmine以获取Rails编码的一些优秀示例。
这样做“选择标签时更改网址”这样的技巧:
function showTab(name, url) {
$('#tab-content-' + name).parent().find('.tab-content').hide();
$('#tab-content-' + name).parent().find('div.tabs a').removeClass('selected');
$('#tab-content-' + name).show();
$('#tab-' + name).addClass('selected');
//replaces current URL with the "href" attribute of the current link
//(only triggered if supported by browser)
if ("replaceState" in window.history) {
window.history.replaceState(null, document.title, url);
}
return false;
}
标签本身将其onclick
设置为"showTab('#{tab[:name]}', this.href); this.blur(); return false;"
。显然,this.href
进入url
,然后进入window.history.replaceState
。