简体   繁体   中英

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. 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.

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

<%= 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]

Which means in route for users#show you need to pass params id as id of user

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

<%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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM