I'm building an events app with users who will each have a personal profile. I've set up a few users for the site but when I try and create and/or edit a profile for each user it refers me back to a flash message "That profile doesn't belong to you!" which is in reference to my first user profile which was set up and works fine.
I'm using Devise gem for initial set up but have built out from their with my own user controller. Here's the code from that controller -
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :set_user
before_action :owned_profile, only: [:edit, :update]
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
end
def edit
@user = current_user #User.find_by(params[:id])
end
def update
@user = User.find_by(params[:id])
if @user.update(user_params)
redirect_to user_path, notice: "Profile successfully updated!"
else
render 'edit'
end
end
private
def user_params
params.require(:user).
permit(:name, :username, :biography, :email, :url)
end
def owned_profile
unless current_user == @user
flash[:alert] = "That profile doesn't belong to you!"
redirect_to root_path
end
end
def set_user
@user = User.find_by(params[:id])
end
end
Any assistance would be appreciated.