简体   繁体   中英

ActionController::UrlGenerationError in Users#show No route matches error

I am creating a users controller to show the users profile page but i am getting this error

ActionController::UrlGenerationError in Users#show
No route matches {:action=>"show", :controller=>"users", :id=>nil}
missing required keys: [:id] error

I am trying to add a profile page for each user so when the user for example goes to

www.mysite.com/users/jack

it will show their profile page

Here's my code:

layouts/_navbar.html.erb

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>

      </button>
      <a class="navbar-brand" href="/">Skillbook</a>




    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">



      </ul>

      <ul class="nav navbar-nav navbar-right">
        <% if user_signed_in? %>
        <td><%= link_to "Profile", user_path(current_user.username) %></td>
        <td><%= gravatar_tag current_user.email, size: 20 %><%= current_user.username %></td>
        <li><%= link_to  "Edit user", edit_user_registration_path %></li>
        <li><%= link_to  "Log out", destroy_user_session_path ,method: :delete %></li>
        <%else%>
        <li><%= link_to  "Log in", new_user_session_path %></li>
        <li><%= link_to  " Sign up", new_user_registration_path%></li>

      <%end%>



      </ul>




      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

users_controller.rb

class UsersController < ApplicationController
before_action :set_user, only: [:show]



private
  def set_user
    @user = User.find_by(username: params[:id])
  end

end

routes.rb

Rails.application.routes.draw do


  get 'pages/home'

  devise_for :users

   root 'pages#home'

resources :users, only: [:show, :index]


end

In url you should pass an id of the user, and you do not do that.

What you really want to incorporate is friendly_id gem .

With it set up you will be able to actually present users id as name(or whatever other attribute/combination of attributes you want):

www.mysite.com/users/1

will become

www.mysite.com/users/jack

Well, this error simply is what it is:

missing required keys: [:id]

Where, according to your setup, is the username of the currently signed-in user.

In a nutshell, what this is telling you is that you are trying to pass in the username of the signed-in user, but for this instance, the user that is signed in has no username .

To fix this, make sure that the username of the signed-in user, (and all other users for that matter) is not nil

In your routes file add

get '/users/:id', to: 'users#show', as: '/users/:username'

The last part as: is where you are naming your route. This would give the user with username 'jack' the route /users/jack. Likewise if you wanted to get rid of users altogether you could put as: '/:username'

No need to install another gem.

Justin

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