1
votes

I am learning ruby on rails by making a project web app, whenever i go to

<%= link_to 'My Profile', user_path(:id) %>
error
Couldn't find User with 'id'=id" is shown...and url is "http://localhost:3000/users/id
If i convert above link to
<%= link_to 'My Profile', user_path %>
, it works but now all other pages except user's show page give error
No route matches {:action=>"show", :controller=>"users"} missing required keys: [:id]
....I cant seem to find any solution anywhere...

Here are my configs:
rake routes

                  Prefix Verb   URI Pattern                       Controller#Action
                    root GET    /                                 static#landing
        new_user_session GET    /users/sign_in(.:format)          devise/sessions#new
            user_session POST   /users/sign_in(.:format)          devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
           user_password POST   /users/password(.:format)         devise/passwords#create
       new_user_password GET    /users/password/new(.:format)     devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)    devise/passwords#edit
                         PATCH  /users/password(.:format)         devise/passwords#update
                         PUT    /users/password(.:format)         devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)           devise/registrations#cancel
       user_registration POST   /users(.:format)                  devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)          devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)             devise/registrations#edit
                         PATCH  /users(.:format)                  devise/registrations#update
                         PUT    /users(.:format)                  devise/registrations#update
                         DELETE /users(.:format)                  devise/registrations#destroy
       user_confirmation POST   /users/confirmation(.:format)     devise/confirmations#create
   new_user_confirmation GET    /users/confirmation/new(.:format) devise/confirmations#new
                         GET    /users/confirmation(.:format)     devise/confirmations#show
                    user GET    /users/:id(.:format)              users#show

_header.html.erb <%= link_to 'My Profile', user_path %>

users_controller.rb class UsersController < ApplicationController def show @user = User.find(params[:id]) end end

routes.rb Rails.application.routes.draw do root 'static#landing' devise_for :users resources :users, :only => [:show]

2

2 Answers

2
votes

You need to pass the id of a user to user_path, not the symbol :id - so something like

<%= link_to 'My Profile', user_path(@current_user.id) %>

if the user you want to link to is in an instance variable - or since you are using Devise and it provides the current_user helper you might be wanting

<%= link_to 'My Profile', user_path(current_user.id) %>
0
votes

I think the first thing wrong is this:

<%= link_to 'My Profile', user_path(:id) %>

You are using a symbol, not a variable

<%= link_to 'My Profile', user_path(@id) %>

Would have an actual variable, assuming you have set @id in your controller.

Normally however you would not set the id for a user in a path - it's a recipe for someone to just change the number manually and view someone elses.

Lets assume you set a session variable called :user_id, then you should do something adding a collection route - so no id is sent.

 resources :users do 
     get :show_profile, on: :collection
 end

This gets you a path to your show_profile action, called with

link_to 'My Profile', show_profile_user_path

Then in your controller do your user look up from the session - so something like:

 @user = User.find_by id: session[:user_id]

I don't know how devises does things, but please don't trust a user to not fiddle with your parameters!