Adding user profiles

Right now we have the ability to edit our own user profiles (our email address, name, and pw) but what if we wanted to be able to click on a user’s name and view a profile page for them?  That’s what we’re doing here.

I don’t need to create a whole new scaffolding for users like I did for pins, because a scaffolding allows us to CRUD (create, read, update, destroy) and we can already create users, delete users, etc.  What we want to do is modify the Devise gem, so if you Google search for “show page Devise” there is a StackOverflow question that helps us do this very thing.

First thing is running:

$ rails generate controller User show

This creates a user controller file automatically  us and the show action is blank as you can see here:

class UsersController < ApplicationController
  def show
  end
end

Then I go into my pins controller and copy the top line of the show action, which is:

@pin = Pin.find(params[:id])

And change pin to user in that code and put it in that empty show action in the Users controller.

Then I open up the Users-> Show view (which was automatically created for us) and replace the contents with this (which doesn’t show up well when pasted in WordPress).  Now we edit our routes file and under devise_for :users we put in

match 'users/:id' => 'users#show', as: :user

Finally in our Pin view we replace part of the code:

 .
  .
  .
        <strong>
            Posted by <%= link_to pin.user.name, pin.user %>
        </strong>
  .
  .
  .

Now clicking on someone’s name on the index page brings up a user profile showing all their pins.

Screen Shot 2013-04-08 at 5.35.41 PM

Leave a comment