Rails Girls Guides

Getting Started with Rails

                             posted by-manishsingh27598@gmail.com

1.Add devise gem

Open up your Gemfile and add this line


gem 'devise'

and run


bundle install

to install the gem. Also remember to restart the Rails server

2.2.Set up devise in your app

Run the following command in the terminal.


rails g devise:install

3.3.Configure Devise

Ensure you have defined default url options in your environments files. Open up config/environments/development.rb and add this line:


   config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

before the end keyword.
Open up app/views/layouts/application.html.erb and add:


<% if notice %>
  <p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
  <p class="alert alert-d
anger"><%= alert %></p>
<% end %>

right above


   <%= yield %>

Open up app/views/ideas/show.html.erb and remove the line that says:


<p id="notice"><%= notice %></p>

Do the same for app/views/comments/show.html.erb. These lines are not necessary as we’ve put the notice in the app/views/layouts/application.html.erb file.

4.4.Setup the User model

We’ll use a bundled generator script to create the User model.


   rails g devise user
   rails db:migrate

Coach: Explain what user model has been generated. What are the fields?

5.5.Create your first user

Now that you have set everything up you can create your first user. Devise creates all the code and routes required to create accounts, log in, log out, etc.
Make sure your rails server is running, open http://localhost:3000/users/sign_up and create your user account.
All we need to do now is to add appropriate links or notice about the user being logged in in the top right corner of the navigation bar.
In order to do that, edit app/views/layouts/application.html.erb add:


<p class="navbar-text pull-right">
<% if user_signed_in? %>
  Logged in as <strong><%= current_user.email %></strong>.
  <%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
  <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link'  %>
<% else %>
  <%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link'  %> |
  <%= link_to "Login", new_user_session_path, :class => 'navbar-link'  %>
<% end %>
</p>

right after


<ul class="nav">
  <li class="active"><a href="/ideas">Ideas</a></li>
</ul>

Finally, force the user to redirect to the login page if the user was not logged in. Open up app/controllers/application_controller.rb and add:


  before_action :authenticate_user!

after protect_from_forgery with: :exception.
Open your browser and try logging in and out from.

Adding Custom Fields to Devise:-

1.RUN MIGRATION



$: rails generate migration add_name_to_users name:string

    invoke  active_record
    create    db/migrate/20140519054104_add_name_to_users.rb

db/migrate/20140519054104_add_name_to_users.rb

class AddNameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :name, :string
  end
end

class AddNameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :name, :string
    add_column :users, :date_of_birth, :datetime
    add_column :users, :is_female, :boolean, default: false
  end
end



$ rake db:migrate

    == 20140519054104 AddNameToUsers: migrating =======================
    -- add_column(:users, :name, :string)
    -> 0.0016s
    -- add_column(:users, :date_of_birth, :datetime)
    -> 0.0005s
    -- add_column(:users, :is_female, :boolean, {:default=>false})
    -> 0.0029s
    == 20140519054104 AddNameToUsers: migrated (0.0052s) ==============

Step 6: Edit Users

We can now try editing our existing user. Let's start by adding a link to the page where we can edit user's profile:
app/views/layouts/_menu.html.erb

<nav class="navbar navbar-default" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <%= link_to "Demo App", root_path, class: 'navbar-brand' %>
        </div>

        <div class="collapse navbar-collapse" id="navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li><%= link_to 'Posts', '#' %></li>
                <% if current_user %>
                    <li><%= link_to 'Edit Profile',edit_user_registration_path %></li>
                    <li><%= link_to 'Logout', destroy_user_session_path, method: :delete %></li>
                <% else %>
                    <li><%= link_to 'Login', new_user_session_path %></li>
                <% end %>
            </ul>
        </div>
    </div>
</nav>

And add our new fields to this page:
app/views/users/registrations/edit.html.erb

    <h2>Edit <%= resource_name.to_s.humanize %></h2>

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
    <%= devise_error_messages! %>

    <div><%= f.label :name %><br />
        <%= f.text_field :name, autofocus: true %></div>

    <div><%= f.check_box :is_female, {}, true %> <%= f.label :is_female, "Female" %> </div>
    <div><%= f.check_box :is_female, {}, false %> <%= f.label :is_female, "Male" %> </div>

    <div><%= f.label :date_of_birth %><br />
        <%= f.date_select :date_of_birth %></div>

    <div><%= f.label :email %><br />
        <%= f.email_field :email %></div>

    <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
    <% end %>

    <div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
        <%= f.password_field :password, autocomplete: "off" %></div>

    <div><%= f.label :password_confirmation %><br />
        <%= f.password_field :password_confirmation, autocomplete: "off" %></div>

    <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
        <%= f.password_field :current_password, autocomplete: "off" %></div>

    <div><%= f.submit "Update" %></div>
    <% end %>

    <h3>Cancel my account</h3>

    <p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>

    <%= link_to "Back", :back %>

Step 7: Save information

Now, we can try to edit our profile with some more precise information and attempt to save.
editing custom fields in user profile
After you save the information and go back to the page, you will find that none of the custom fields has been saved:
failed to save custom information

Comments

Post a Comment

Popular posts from this blog

Check Your ageInDays by Javascript