[英]ruby on rails - User profil page with devise - routing to show profil page
Hi I'm facing a bug when I try to implement a link_to my profile page on my navbar. 嗨,我在导航栏上尝试实现link_to我的个人资料页面时遇到错误。 it show me a routing error with the missing key id, it only appear when I put the link on my navbar, otherwise I can access to my profile account.
它会显示路由错误,缺少键ID,仅当我将链接放在导航栏上时才会出现,否则我可以访问我的个人资料帐户。
Here is the error: 这是错误:
No route matches {:action=>"show", :controller=>"user"}, missing required keys: [:id]
And this is my code: routes 这是我的代码:路线
get 'user/show/:id', to: 'user#show', as: 'profil'
devise_for :users
root 'home#index'
resources :users
User controller: 用户控制器:
def show
@user = User.find(params[:id])
end
The application.html where I insert the link making crash my app 我在其中插入链接的application.html使我的应用崩溃
<%= link_to 'profil', profil_path , class: "dropdown-item" %>
Thanks in advance. 提前致谢。
Routes amendment: - 路线修改:-
root 'home#index'
devise_for :users
resources :users, except: [:show]
get 'user/show/:id', to: 'users#show', as: 'profil'
missing required keys: [:id]
缺少必需的键:[:id]
Which means in route for users#show
you need to pass params id as id of user 这意味着在
users#show
路线中#show您需要将params id作为user ID传递
Devise has current_user
which is the logged in user object so in the application.htm
Make profil dropdown dynamic say if user is logged in Devise具有
current_user
这是已登录的用户对象,因此在application.htm
使概要文件下拉列表动态显示用户是否已登录
<%if current_user.present?%>
<%= link_to 'profil', profil_path(id: current_user.id) , class: "dropdown-item" %>
<%end%>
Users controller 用户控制器
class UsersController < ApplicationController
before_action :authenticate_user!
def show
@user = User.find(params[:id])
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.