为了路由编辑当前用户的个人资料,我需要使用哪种语法?我想在我的页面上添加一个链接,用于编辑您的用户个人资料。我知道如何link_to但不确定语法是什么来获得当前用户。
<%= link_to "Edit Profile", current_user %>
我的UsersController
看起来像这样:
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
在我的路线文件中,我有:
resources :users
答案 0 :(得分:2)
您可以使用括号表示法
link_to 'Edit profile', [:edit, current_user]
或named routes helpers,您可以通过调用rake routes
link_to 'Edit profile', edit_user_path(current_user)
答案 1 :(得分:1)
简单如下:
link_to 'Edit profile', [:edit, current_user]