mardi 19 janvier 2016

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 enter image description here

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

http://ift.tt/1RxLIDn

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

Aucun commentaire:

Enregistrer un commentaire