Ruby on rails 强制/手动输入后更新URL中的参数

Ruby on rails 强制/手动输入后更新URL中的参数,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,我目前正在使用Desive用户系统设置一个模型导师,该模型导师与一个模型档案相关联,其中导师有一个:档案,档案属于:导师。因此,导师id外键将每个配置文件条目与导师条目相关联 在概要文件模型中,我使用了常用的Rails REST方法:新建、编辑、显示、创建和更新 当通过URI/profiles/:id/edit访问profileedit时,无论在URL中输入了什么来代替:id,都会正确地对配置文件的相应导师模型进行编辑,但是URL不反映这一点似乎有点尴尬 因此,如果使用tutor_id:2和配置

我目前正在使用Desive用户系统设置一个模型导师,该模型导师与一个模型档案相关联,其中导师有一个:档案,档案属于:导师。因此,导师id外键将每个配置文件条目与导师条目相关联

在概要文件模型中,我使用了常用的Rails REST方法:新建、编辑、显示、创建和更新

当通过URI/profiles/:id/edit访问profileedit时,无论在URL中输入了什么来代替:id,都会正确地对配置文件的相应导师模型进行编辑,但是URL不反映这一点似乎有点尴尬

因此,如果使用tutor_id:2和配置文件id:2的tutor登录,则通过/profiles/4/edit或/profiles/afjsdjfdsj/edit访问编辑操作会产生与访问正确的URL/profiles/2/edit相同的结果,并且仍然会更新正确的tutor配置文件

我想做的是更新地址栏中的URL以反映配置文件id:2。我尝试在ProfilesController的编辑方法中使用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

在经历了许多挫折之后,我发现params[:id]返回的是字符串而不是整数,因此需要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 
#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