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

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:

            Posted by <%= link_to, pin.user %>

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


User database with Devise and the simple form gem

Up until now, my simple website has had ‘login’ and ‘logout’ buttons but they haven’t actually been functional because there is no concept of a database setup yet.  The two videos I watched today helped me not only install the Devise gem to add database functionality to my site, but also use the simple form gem to make my forms look good and match Bootstrap.

Screen Shot 2013-03-31 at 6.37.10 PM

I don’t know that it makes sense to walk through these because the help documentation for both of those gems was pretty easy to follow but there were a couple of things I want to make sure I remember.

The first is that for simple_form, I needed to use the –bootstrap modifier to get the version I needed that works with Bootstrap.

Also, here is what I had to do to make the forms look good:

At the top of the edit.html.erb and new.html.erb files, I had to  change form_for to simple_form_for.  I also had to add the class form-horizontal, so now the code at the top looks like this:

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %>

Next I had to get rid of all long mumbo jumbo for the fields and simplified them with f.input.  This makes it look like:

<%= f.input :name %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation %>

Finally, I had to change the <div> in front of the buttons to   <div class= “form-actions”> and add class: ‘btn btn-primary’  to all of the buttons.  This makes it look like so:

<div class= "form-actions"><%= f.submit "Sign up", class: 'btn btn-primary' %></div>
<% end %>

The other thing is that Devise’s notifications aren’t very attractive out of the box, so to switch to Bootstrap’s notifications I had to do <%= f.error_notification %> at the top instead of Devise notifications.

Deploying to Heroku for the first time with Devise:

I need to remember to run this in Terminal otherwise none of the user functionality will work: heroku run rake db:migrate.

Adding user fields to my project with Devise:

First I have to run in Terminal:

$ rails generate migration AddNameToUsers name:string


$ rake db migrate

In app => models => user.rb I need to make my new “name” field accessible, so I add it to the attr_accessible block of code.

Next I have to actually ASK the user for their name, so I go to new.html.erb and add in <%= f.input :name %> there.  And of course I’d have to do that in the edit_registration file too, but that’s kind of self-explanatory.

The only other thing I did was make ‘members only content’ which I did by using some Ruby code <% if user_signed_in? %>. That code on my homepage looks like:

<% if user_signed_in? %>
<h1>Congrats, you're logged in!</h1>
<% else %>
<div class='hero-unit'>
<h1>Welcome to One Month Rails!</h1>
You found the homepage for the <%= link_to "One Month Rails", "" %> application.
<%= link_to "Sign Up Now!", new_user_registration_path, class: "btn btn-primary btn-large" %>
<% end %>

My application is live here now:

Though right now, you can just sign up, login, edit your registration.  There isn’t a whole lot of anything up there, but apparently I’ll be making a type of simple Pinterest soon starting with the very next lesson in One Month Rails. 🙂