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

then

$ 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>
<p>
You found the homepage for the <%= link_to "One Month Rails", "http://onemonthrails.com" %> application.
</p>
<p>
<%= link_to "Sign Up Now!", new_user_registration_path, class: "btn btn-primary btn-large" %>
</p>
</div>
<% end %>

My application is live here now:

http://young-basin-3485.herokuapp.com/

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. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s