[英]ruby on rails - User profil page with devise - routing to show profil page
嗨,我在導航欄上嘗試實現link_to我的個人資料頁面時遇到錯誤。 它會顯示路由錯誤,缺少鍵ID,僅當我將鏈接放在導航欄上時才會出現,否則我可以訪問我的個人資料帳戶。
這是錯誤:
No route matches {:action=>"show", :controller=>"user"}, missing required keys: [:id]
這是我的代碼:路線
get 'user/show/:id', to: 'user#show', as: 'profil'
devise_for :users
root 'home#index'
resources :users
用戶控制器:
def show
@user = User.find(params[:id])
end
我在其中插入鏈接的application.html使我的應用崩潰
<%= link_to 'profil', profil_path , class: "dropdown-item" %>
提前致謝。
路線修改:-
root 'home#index'
devise_for :users
resources :users, except: [:show]
get 'user/show/:id', to: 'users#show', as: 'profil'
缺少必需的鍵:[:id]
這意味着在users#show
路線中#show您需要將params id作為user ID傳遞
Devise具有current_user
這是已登錄的用戶對象,因此在application.htm
使概要文件下拉列表動態顯示用戶是否已登錄
<%if current_user.present?%>
<%= link_to 'profil', profil_path(id: current_user.id) , class: "dropdown-item" %>
<%end%>
用戶控制器
class UsersController < ApplicationController
before_action :authenticate_user!
def show
@user = User.find(params[:id])
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.