在 forced/manual 输入后更新 URL 中的参数
Updating params in URL after forced/manual input
我目前正在使用 Devise 用户系统设置模型 Tutor
,该模型与模型 Profile
相关联,其中 Tutor has_one :profile
和 Profile belongs_to :tutor
。因此,tutor_id 外键将每个配置文件条目关联到一个导师条目。
在 Profile
模型中,我有通常的 Rails REST 方法 new
、edit
、show
、create
和 update
.
当通过 URI /profiles/:id/edit
访问 profile#edit
时,无论在 [=57] 中代替 :id
输入什么内容,都会对个人资料的相应导师模型进行正确编辑=],但 URL 不反映这一点似乎有点尴尬。
因此,如果 tutor
与 tutor_id: 2
和配置文件 id: 2
已登录,则通过 /profiles/4/edit
或 [=33= 访问 edit
操作] 以某种方式产生与访问正确的 URL /profiles/2/edit
相同的结果,并且仍将更新正确的导师个人资料。
我想做的是更新地址栏中的 URL 以反映配置文件 id: 2
。我尝试在 ProfilesController
的 edit
方法中使用 redirect_to
无济于事:
#app/assets/controllers/profiles_controller.rb
def edit
...
@profile = current_tutor.profile
if @profile.id != params[:id]
redirect_to edit_profile_path(@profile.id)
end
...
end
这里是profiles_controller.rb
# app/assets/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
before_action :authenticate_tutor!, except: [:show]
# Everything handled by edit page
def new
@tutor = current_tutor
if (@tutor.profile.nil?)
@profile = @tutor.build_profile
else
@profile = @tutor.profile
redirect_to edit_profile_path(@profile.id)
end
end
def edit
@tutor = current_tutor
if (@tutor.profile.nil?)
redirect_to new_profile_path
else
@profile = @tutor.profile
end
end
def show
if tutor_signed_in?
@tutor = Tutor.find(current_tutor.id)
@profile = Profile.find(@tutor.profile.id)
else
@profile = Profile.find(params[:id])
end
end
# POST /tutors
# POST /tutors.json
def create
@profile = current_tutor.build_profile(profile_params)
if @profile.save
flash[:success] = "Profile created!"
redirect_to tutors_dashboard_path
else
render 'new'
end
end
# PATCH/PUT /tutors/1
# PATCH/PUT /tutors/1.json
def update
@profile = Profile.find(current_tutor.id)
if @profile.update(profile_params)
flash[:success] = "Profile updated!"
redirect_to tutors_dashboard_path
else
render 'edit'
end
end
private
def profile_params
params.require(:profile).permit(:first_name, :last_name, :postal_code, :gender, :dob, :rate, :alma_mater, :major, :degree, :address, :phone_num, :travel_radius, :bio)
end
end
这是routes.rb
Rails.application.routes.draw do
root 'pages#home'
get '/about' => 'pages#about'
get '/contact' => 'pages#contact'
get '/about-tutors' => 'pages#about_tutors'
get '/about-students' => 'pages#about_students'
devise_for :tutors, controllers: {
confirmations: 'tutors/confirmations',
passwords: 'tutors/passwords',
registrations: 'tutors/registrations',
sessions: 'tutors/sessions'
}
get '/tutors/dashboard' => 'tutors#dashboard'
resources :profiles
end
经过多次挫折后,我发现 params[:id]
returns 是一个字符串而不是整数,因此需要 to_i
来比较两者。
正确代码:
#app/assets/controllers/profiles_controller.rb
def edit
...
@profile = current_tutor.profile
if @profile.id != params[:id].to_i # convert params[:id] to an integer value
redirect_to edit_profile_path(@profile.id)
end
...
end
我目前正在使用 Devise 用户系统设置模型 Tutor
,该模型与模型 Profile
相关联,其中 Tutor has_one :profile
和 Profile belongs_to :tutor
。因此,tutor_id 外键将每个配置文件条目关联到一个导师条目。
在 Profile
模型中,我有通常的 Rails REST 方法 new
、edit
、show
、create
和 update
.
当通过 URI /profiles/:id/edit
访问 profile#edit
时,无论在 [=57] 中代替 :id
输入什么内容,都会对个人资料的相应导师模型进行正确编辑=],但 URL 不反映这一点似乎有点尴尬。
因此,如果 tutor
与 tutor_id: 2
和配置文件 id: 2
已登录,则通过 /profiles/4/edit
或 [=33= 访问 edit
操作] 以某种方式产生与访问正确的 URL /profiles/2/edit
相同的结果,并且仍将更新正确的导师个人资料。
我想做的是更新地址栏中的 URL 以反映配置文件 id: 2
。我尝试在 ProfilesController
的 edit
方法中使用 redirect_to
无济于事:
#app/assets/controllers/profiles_controller.rb
def edit
...
@profile = current_tutor.profile
if @profile.id != params[:id]
redirect_to edit_profile_path(@profile.id)
end
...
end
这里是profiles_controller.rb
# app/assets/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
before_action :authenticate_tutor!, except: [:show]
# Everything handled by edit page
def new
@tutor = current_tutor
if (@tutor.profile.nil?)
@profile = @tutor.build_profile
else
@profile = @tutor.profile
redirect_to edit_profile_path(@profile.id)
end
end
def edit
@tutor = current_tutor
if (@tutor.profile.nil?)
redirect_to new_profile_path
else
@profile = @tutor.profile
end
end
def show
if tutor_signed_in?
@tutor = Tutor.find(current_tutor.id)
@profile = Profile.find(@tutor.profile.id)
else
@profile = Profile.find(params[:id])
end
end
# POST /tutors
# POST /tutors.json
def create
@profile = current_tutor.build_profile(profile_params)
if @profile.save
flash[:success] = "Profile created!"
redirect_to tutors_dashboard_path
else
render 'new'
end
end
# PATCH/PUT /tutors/1
# PATCH/PUT /tutors/1.json
def update
@profile = Profile.find(current_tutor.id)
if @profile.update(profile_params)
flash[:success] = "Profile updated!"
redirect_to tutors_dashboard_path
else
render 'edit'
end
end
private
def profile_params
params.require(:profile).permit(:first_name, :last_name, :postal_code, :gender, :dob, :rate, :alma_mater, :major, :degree, :address, :phone_num, :travel_radius, :bio)
end
end
这是routes.rb
Rails.application.routes.draw do
root 'pages#home'
get '/about' => 'pages#about'
get '/contact' => 'pages#contact'
get '/about-tutors' => 'pages#about_tutors'
get '/about-students' => 'pages#about_students'
devise_for :tutors, controllers: {
confirmations: 'tutors/confirmations',
passwords: 'tutors/passwords',
registrations: 'tutors/registrations',
sessions: 'tutors/sessions'
}
get '/tutors/dashboard' => 'tutors#dashboard'
resources :profiles
end
经过多次挫折后,我发现 params[:id]
returns 是一个字符串而不是整数,因此需要 to_i
来比较两者。
正确代码:
#app/assets/controllers/profiles_controller.rb
def edit
...
@profile = current_tutor.profile
if @profile.id != params[:id].to_i # convert params[:id] to an integer value
redirect_to edit_profile_path(@profile.id)
end
...
end