Ruby Gems

Devise CanCan

Ajax

RubyGems

RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a "gem"), a tool designed to easily manage the installation of gems, and a server for distributing them.

Example

source 'https://rubygems.org'

gem 'rails', '4.2.3'
gem 'pg', '0.18.4'

gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails', '~> 4.1.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'haml-rails', '0.9.0'
gem 'sass-rails', '~> 5.0'
gem 'bootstrap-generators', '~> 3.3.4'
gem "paperclip", "~> 4.3"
gem 'kaminari-bootstrap', '~> 3.0.1'

gem 'devise', '3.4.1'
gem 'cancancan', '1.9.2'
gem 'rolify', '3.4.1'
gem 'rails_admin', '~> 0.8.1'

group :production do
  gem 'unicorn'
end

group :development, :test do
  gem 'rspec-rails', '~> 3.0'
  gem 'factory_girl_rails', '4.5.0'
end

Working with Gems

# Installation:
$ gem install mygem

# Uninstallation:
$ gem uninstall mygem

# Listing installed gems:
$ gem list—local

# Listing available gems, e.g.:
$ gem list—remote

# Create RDoc documentation for all gems:
$ gem rdoc—all

# Adding a trusted certificate:
$ gem cert -a 

# Download but do not install a gem:
$ gem fetch mygem

# Search available gems, e.g.:
gem search STRING—remote

Authentication and

authorization

Devise + CanCanCan + Rolify

 

Gemfile

gem 'devise'
gem 'cancancan'
gem 'rolify'

1. run bundle install to install all required gems

$ bundle install

2. Run Devise generator

$ rails generate devise:install

3. Create the User model from Devise

$ rails generate devise User

4. Create the Ability class from CanCanCan 

$ rails generate cancan:ability

5. Create the Role class from rolify 

$ rails generate rolify Role User

6. Run migrations 

$ rake db:migrate

Manage user roles

$ user = User.last

# add role
$ user.add_role :admin

# remove role
$ user.remove_role :admin

# check role
$ user.has_role? :admin
# => true / false

# list user roles
$ user.roles
# => [ list of user roles ]
before_add
after_add
before_remove
after_remove
class User < ActiveRecord::Base
  rolify :before_add => :before_add_method

  def before_add_method(role)
    # do something before it gets added
  end
end

Add/remove/list roles

Example callback

Callbacks list

Devise configuration

In your controller

class PostsController < ApplicationController
  before_action :authenticate_user!

  def index
    # ...
  end
end

To verify if a user is signed in, use the following helper:

user_signed_in?

For the current signed-in user, this helper is available:

current_user

You can access the session for this scope:

user_session

Example App

Rails + Ajax


<%= form_for(@post, remote: true) do |f| %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>
  <div class="field">
    <%= f.label :category_id %><br>
    <%= f.select :category_id, Category.all.collect {|c| [ c.name, c.id ] }%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Your form with "remote: true"

Rails + Ajax

$('#posts').html("<%= escape_javascript(render 'posts/posts') %>");

For rendering part of page

...
<tbody id="posts">
    <%= render partial: 'posts' %>
</tbody>
...

.../app/views/posts/search.html.erb

.../app/views/posts/search.js.erb

<% @posts.each do |post| %>
  <tr>
    <td><%= post.title %></td>
    <td><%= post.content %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy', post, method: :delete %></td>
  </tr>
<% end %>

.../app/views/posts/_posts.html.erb

Rails + Ajax


class PostsController < ApplicationController
  ...

  def search
    if params[:search]
      @posts = Post.where("title LIKE :search", {:search => "#{params[:search]}%"})
    else
      @posts = Post.all
    end
    respond_to do |format|
      format.html
      format.js
    end
  end
  ...
end

.../app/controllers/posts_controller.rb

Example App

Made with Slides.com