ruby on rails - Updating params in URL after forced/manual input -
i using devise user system set model tutor associated model profile whereby tutor has_one :profile , profile belongs_to :tutor. therefore tutor_id foreign key associates each profile entry tutor entry.
in profile model, have usual rails rest methods new, edit, show, create , update.
when accessing profile#edit via uri /profiles/:id/edit, edits made correctly corresponding tutor model of profile regardless of what's entered in place of :id in url, seems kind of awkward url not reflect this.
so if tutor tutor_id: 2 , profile id: 2 signed in, accessing edit action via /profiles/4/edit or /profiles/afjsdjfdsj/edit somehow produces same result accessing proper url /profiles/2/edit , still update correct profile of tutor.
what want update url in address bar reflect profile id: 2. i've tried using redirect_to in edit method in profilescontroller no avail:
#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 here profiles_controller.rb
# app/assets/controllers/profiles_controller.rb class profilescontroller < applicationcontroller before_action :authenticate_tutor!, except: [:show] # handled 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 here routes.rb
rails.application.routes.draw root 'pages#home' '/about' => 'pages#about' '/contact' => 'pages#contact' '/about-tutors' => 'pages#about_tutors' '/about-students' => 'pages#about_students' devise_for :tutors, controllers: { confirmations: 'tutors/confirmations', passwords: 'tutors/passwords', registrations: 'tutors/registrations', sessions: 'tutors/sessions' } '/tutors/dashboard' => 'tutors#dashboard' resources :profiles end
after frustration, figured out params[:id] returns string , not integer, therefore to_i required compare two.
correct code:
#app/assets/controllers/profiles_controller.rb def edit ... @profile = current_tutor.profile if @profile.id != params[:id].to_i # convert params[:id] integer value redirect_to edit_profile_path(@profile.id) end ... end
Comments
Post a Comment