0

Here are my routes:

Stynyl::Application.routes.draw do
  resources :things

  devise_for :users
  get '/about', to: 'pages#about'
  root 'things#index'

  get 'users/:username' => "users#show"
end

Here is Users controller:

class UsersController < ApplicationController
  def show
    @user = User.find_by_name(params[:username])
  end
end

Here is my users/show.html.erb

<p>
  <strong><%= @user.username %></strong>
</p>

I know the view is basic, but I just want to see if it will print out the name of the user.

I created a user with the username 'delacram'. When I type localhost:3000/users/delacram, I get the error presented in the title.

What's the problem?

My User model

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :things

  validates :name, presence: true
  validates :username, presence: true
  validates :email, presence: true
end
Dylan Richards
  • 708
  • 1
  • 13
  • 33

2 Answers2

2

set_user is not called automatically.

You can add a before_filter:

class UsersController < ApplicationController
  before_filter :set_user, only: :show

  def set_user
    @user = User.find_by_name(params[:username])
  end
end

Or simply set your user within show action:

class UsersController < ApplicationController
  def show
    @user = User.find_by_name(params[:username])
  end
end

As @coletrain mentioned, look into friendly_id gem. Your solution will work, but it requires further tweaks. Don't forget to add an index to your username column.

Damien Roche
  • 13,189
  • 18
  • 68
  • 96
  • I have gone with the second option, using the show method. But how do I add an index to the username column? @Damien Roche – Dylan Richards Nov 28 '13 at 22:12
  • Great. I prefer the second option with it being a simple controller. See here for a good explanation on creating an index on an existing column: http://stackoverflow.com/questions/15881749/adding-index-after-adding-column-rails (simple migration) – Damien Roche Nov 28 '13 at 22:14
  • I'm now getting an error that says `"undefined method `username' for nil:NilClass"` – Dylan Richards Nov 28 '13 at 22:15
  • @DylanRichards also, though I'm sure you might have considered, ensure the username column is unique. – Damien Roche Nov 28 '13 at 22:16
  • @DylanRichards where is that error coming from? View? Where is username called? Your view in the question shows `@user.name`, as in `name` attribute. – Damien Roche Nov 28 '13 at 22:17
  • I have updated the question with the controller and view I currently have. The error I am receiving now is undefined method `name' for nil:NilClass. I am getting this error when I go to localhost:3000/users/delacram – Dylan Richards Nov 28 '13 at 22:22
  • 1
    @DylanRichards appears there is a problem elsewhere. Could you update with your user model? I'd also suggest you open up a Rails console to test your code: `User.find_by_name('delacram')` to quickly debug possible query issues. – Damien Roche Nov 28 '13 at 22:26
  • Updated original post with User model. Will try console command now. @Damien Roche – Dylan Richards Nov 28 '13 at 22:28
  • Ran the console command. It appears I must use find_by_username instead of find_by_name. Furthermore, capitalization matters in the URL, which I didn't ever think was the case. I'll update my controller accordingly. Problem == solved. Thanks, Damien! – Dylan Richards Nov 28 '13 at 22:31
  • @DylanRichards of course! I should have noticed that! Glad you got it working :) – Damien Roche Nov 28 '13 at 22:33
  • @DylanRichards could you mark this as correct if it has answered the question? – Damien Roche Nov 30 '13 at 15:13
  • Forgot about that. Sorry. It definitely answered my question. @Damien Roche – Dylan Richards Nov 30 '13 at 15:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42246/discussion-between-dylan-richards-and-damien-roche) – Dylan Richards Nov 30 '13 at 15:14
  • I have another question that has gone unanswered for 17 hours. Mind having a look at it? Or coming to chat to discuss it? I beg you. – Dylan Richards Nov 30 '13 at 15:15
  • Cheers @DylanRichards. I'll take a look at your questions first and let you know if I can help. – Damien Roche Nov 30 '13 at 15:27
  • Okay! Thank you so much. Here is the link to it http://stackoverflow.com/questions/20293474/undefined-method-username-for-nilnilclass-when-signing-up-and-signing-in @Damien – Dylan Richards Nov 30 '13 at 15:28
  • @DylanRichards also, another great resource you should leverage is irc (especially for realtime feedback). Check out the rubyonrails channel. – Damien Roche Nov 30 '13 at 15:29
0

For friendly urls such as yoursite.com/user/delacram you will need to use a gem such as friendlyId which handles that.

Check out this railscast episode

coletrain
  • 2,809
  • 35
  • 43