Blogger11 Blogger11 - 4 months ago 9
Ruby Question

Route to Current User Edit in a view

What is the syntext I need to use in order to route to edit the current user's profile? I want to put a link on my page that takes you to edit your user profile. I know how to link_to but not sure what what the syntex is to get to the current user.



My Users controller looks like this:

class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy


def index
@users = User.paginate(page: params[:page])
end

def new
@user = User.new
end

def create
@user = User.new(user_params)

if @user.save
flash[:notice] = "Welcome to Studio Academy Manager"
sign_in @user
redirect_to root_url
else
render 'new'
end
end

def show
@user = User.find(params[:id])
end

def edit
@user = User.find(params[:id])
end

def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end

def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end

private
def user_params
params.require(:user).permit(:email, :username, :password, :password_confirmation, :first_name, :last_name, :phone, :street_1, :street_2, :city, :state, :zip, :enabled, :admin)
end

def logged_in_user
unless current_user.present?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end

def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless @user == current_user
end

def admin_user
redirect_to(root_url) unless current_user.admin?
end
end


And In my routes file I have:

resources :users

Answer

As simple as:

link_to 'Edit profile', [:edit, current_user]
Comments