0

I am creating User Profiles with vanity URLs that use the username. The actual profile page works well, however I have my root page as the profile page if the user is signed in. If they are redirected to root then they get the error Couldn't find User without an ID and shows this code as the error pointing to the @user line...

class ProfilesController < ApplicationController

    def show
        @current_user = current_user
        @user = User.friendly.find(params[:id])
        @username = "@" + @user.username
        @posting = Posting.new
    end

end

Here is my routes file as well...

devise_for :users
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".
  get "profiles/show"

  devise_scope :user do
    get '/register', to: 'devise/registrations#new', as: :register
    get '/login', to: 'devise/sessions#new', as: :login
    get '/logout', to: 'devise/sessions#destroy', as: :logout
    get '/edit', to: 'devise/registrations#edit', as: :edit
  end

  authenticated :user do
    devise_scope :user do
      root to: "profiles#show", :as => "authenticated"
    end
  end

  unauthenticated do
    devise_scope :user do
      root to: "devise/sessions#new", :as => "unauthenticated"
    end
  end

  get '/:id' => 'profiles#show', as: :profile
Taylor Mitchell
  • 614
  • 11
  • 27

1 Answers1

2

This is not the problem with friendly id gem. The problem is that you are redirecting to a show method without supplying id, hence params[:id] is nil.

You can go around this by changing your show method:

def show
  @current_user = current_user # why do you need this?
  @user = params[:id] ? User.friendly.find(params[:id]) : current_user
  @username = "@" + @user.username
  @posting = Posting.new
end
BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • I didn't really need the current user. I had someone looking at it and he put that in. I have no idea why but I just left it in. Thank you! I'm going to test it. How does that work or is there a place to learn those types of work arounds? – Taylor Mitchell Mar 08 '14 at 21:01
  • It works! I knew it wasn't the gem. I never learned things like that yet. I'm fairly new to rails(1.5mths) – Taylor Mitchell Mar 08 '14 at 21:05
  • 1
    @TaylorMitchell No worries, I hated rails after my first 1.5mth. Btw. it would be probably a better idea to have a separate action like `profile` for displaying current_user. `Show` action should always be executed with an id. – BroiSatse Mar 08 '14 at 21:10